In PHP 7, a new feature, null coalescing operator (??) has been introduced. It is used to replace the 三元与isset()函数结合使用的操作。这个无效的如果合并运算符存在且不为NULL,则返回第一个操作数;否则返回第二个操作数。
<?php
// fetch the value of $_GET['user'] and returns 'not passed'
// if username is not passed
$username = $_GET['username'] ?? 'not passed';
print($username);
print("<br/>");
// Equivalent code using ternary operator
$username = isset($_GET['username']) ? $_GET['username'] : 'not passed';
print($username);
print("<br/>");
// Chaining ?? operation
$username = $_GET['username'] ?? $_POST['username'] ?? 'not passed';
print($username);
?>
它产生以下浏览器输出–
not passed
not passed
not passed