This article will introduce to you the difference between "??" and "?:" introduced in PHP7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Test code
Input test:
<?php $array = [ 'a' => 1, 'b' => 2, 'c' => [], ]; $a = $array['c'] ?? 0; $b = $array['c'] ?: 0; $c = $array['d'] ?? 0; $d = $array['d'] ?: 0; $e = $array['c'] ? 1 : 0; $f = isset($array['c']) ? 1 : 0; $g = $array['d'] ? 1 : 0; $h = isset($array['d']['e']) ? 1 : 0; $i = !empty($array['c']) ? 1 : 0; $j = !empty($array['d']) ? 1 : 0; var_dump($a); var_dump($b); var_dump($c); var_dump($d); var_dump($e); var_dump($f); var_dump($g); var_dump($h); var_dump($i); var_dump($j);
Output result:
PHP Notice: Undefined index: d in /home/fanyu/abc.php on line 11 PHP Notice: Undefined index: d in /home/fanyu/abc.php on line 14 array(0) { } int(0) int(0) int(0) int(0) int(1) int(0) int(0) int(0) int(0)
Conclusion
$a ?? 0 is equivalent to isset($a) ? $a : 0.
$a ?: 0 is equivalent to $a ? $a : 0.
empty: Determine whether a variable is empty (null, false, 00, 0, '0', ‖ and the like will all return true).
isset: Determine whether a variable is set (values such as false, 00, 0, '0', ‖ will also return true).
Recommended learning: php video tutorial
The above is the detailed content of What is the difference between '??' and '?:' introduced in PHP7. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version