PHP's ternary operator

PHP ternary operator is a kind of comparison operator, which is widely used in development. In addition, the ternary operator has many shorthand patterns.

$a = 0;    
$b = $a > 0  ? $a : 1;    
$c = $a ? $a : 2;    
$d = $a ?: 3;    
$f = $a ?? 3;    
var_dump($b); // int 1  true返回$a, false返回1
var_dump($c); // int 2  true返回$a, false返回2
var_dump($d); // int 3  true返回$a, false返回3
var_dump($f); // int 0  将最后的表达式转化后的表达式 isset($a) ? $a : 3  

Guess you like

Origin blog.csdn.net/qq_41526316/article/details/89562146