PHP中的三元运算符 [英] The Ternary Operator in PHP
本文介绍了PHP中的三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$chow = 3;
echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three";
输出:三个
$chow = 1;
echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three";
输出:两个
有人可以解释为什么$ chow = 1而不是"one"时输出为"2"吗?
Can anyone explain why the output is "two" when $chow = 1 instead of "one"?
推荐答案
This is because the ternary operator (?:
) is left associative so this is how it's getting evaluated:
((1 == 1) ? "one" : (1 == 2)) ? "two" : "three"
所以1 == 1
-> TRUE
的意思是:
"one" ? "two" : "three"
和"one"
-> TRUE
,因此输出为:
And "one"
-> TRUE
so the output will be:
two
这篇关于PHP中的三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文