php - 在回声中使用三元运算符?
问题描述
我怎么可以这样使用回声:
<?php echo false ? 'yes' : 'no'; ?> //no
但是不能这样使用
<?php echo false ?? 'yes'; ?> //nothing output
解决方案
这 ??PHP 中的运算符是 Null 合并运算符:
expr1 ?? expr2;
expr1 is returned when expr1 exists and is NOT null; otherwise it returns expr2.
由于在这种情况下 expr1 为假但已设置,因此此表达式返回布尔值假。
比较:
echo false ?? 'It is FALSE'; // won't be displayed
echo null ?? 'it is NULL'; // It will work
当传递布尔值 false 时,Echo 不输出。
推荐阅读
- python - 使用 pyspark 会话从本地文件读取时如何跳过一些行?
- c# - 带有文件和正文的 WebAPI 方法
- python - Python numpy for 循环,从列表创建列表并连接
- bootstrap-4 - 网格系统中的 Bootstrap 4 间距问题
- javascript - window.onpopstate 不起作用任何警报或控制台错误
- excel - 逐行,如果单元格 H= 单元格 I,则删除整行
- sql-server - 识别最高可能的数据类型
- c# - base64 编码是否对编码有任何字符串长度限制?
- android - 房间失效跟踪器在 1.1.1 中被初始化了两次
- javascript - 为什么 input[type=radio] 的默认值是 'on' 而不是 ''?