PHP7 - Null合并运算符

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
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3