在PHP 7中,引入了一个新函数空合并运算符(??)。它与isset()函数一起用于替换三元操作。 Null 合并运算符如果存在且不为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); ?>
它产生以下浏览器输出-
链接:https://www.learnfk.com/php7+/php7-coalescing-operator.html
来源:LearnFk无涯教程网
not passed not passed not passed
这一章《PHP 7 - 空合并运算符》你学到了什么?在下面做个笔记吧!做站不易,你的分享是对我们最大的支持,感谢!😊
如何在 wordpress 项目 cpanel 中集成 laravel8 项目?
Visual Studio Code PHP Intelephense 不断显示不必要的错误
Google reCaptcha 响应“未捕获(promise )为空”