Home >Backend Development >PHP7 >in PHP7? and? ? What's the difference
This article will introduce to you the difference between "?" and "??" in PHP7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
$a = ''; // or 0 or false $b = $a ?? 'a'; // 此时会判断$a是否存在 $a不为null // 等价于 // $b = isset($a) ? $a : 'a'; // $b is '' or 0 or false $c = $a ?: 'a'; // 此时会判断$a的值 // $c is 'a'
$a = null; $b = $a ?? 'a'; // 此时$a为null // $b is 'a' $c = $a ?: 'a'; // $c is 'a'
$a = null; $b = 'b'; $c = $a ?? $b ?? 'c'; // 返回第一个有定义的值 // $c is 'b' $a = null; $b = null; $c = $a ?? $b ?? 'c'; // $c is 'c'
function getId(?int $id) { return $id; } // 参数为指定的整型或空值 getId(857); // 857 getId('857'); // 参数非整型 报错 getId(); // 参数空 报错 getId(''); // 参数非整型 报错 getId(0); // 0 getId(null); // null
function getId():?int { return 1; } // 返回值为指定的整型或空值 getId() // 1 function getId():?int { return null; } getId() // null function getId():?int { return '1'; } getId() // 返回非整型 报错
Recommended learning: php video tutorial
The above is the detailed content of in PHP7? and? ? What's the difference. For more information, please follow other related articles on the PHP Chinese website!