PHP7一个新的功能,空合并运算符(??)已经推出。它是用来与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 |