為什么我可以這樣使用 echo:<?php echo false ? 'yes' : 'no'; ?> //no但不能這樣使用<?php echo false ?? 'yes'; ?> //nothing output
2 回答

慕桂英546537
TA貢獻1848條經驗 獲得超10個贊
這 ??PHP 中的運算符是空合并運算符:
expr1 ?? expr2; expr1 is returned when expr1 exists and is NOT null; otherwise it returns expr2.
由于在本例中 expr1 為 false 但已設置,因此該表達式返回布爾值 false。
比較:
echo false ?? 'It is FALSE'; // won't be displayed echo null ?? 'it is NULL'; // It will work
當傳遞布爾值 false 時,Echo 不會輸出。

Cats萌萌
TA貢獻1805條經驗 獲得超9個贊
如前所述,您在這里使用空合并運算符
它正在檢查 false 是否為空
只是想添加:常規與簡寫三元運算
echo false ? 'yes' : 'no';
是相同的
echo false ?: 'no';
所以,
echo true ?: 'no';
將輸出 1,因為 ?: 本質上跳過第一個表達式
(condition) ? expression1 : expression2
- 2 回答
- 0 關注
- 170 瀏覽
添加回答
舉報
0/150
提交
取消