在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.comhttps://www.learnfk.com/php7+/php7-coalescing-operator.html
来源:LearnFk无涯教程网
not passed not passed not passed
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕