The Ternary Operator in PHP [duplicate]

天大地大妈咪最大 提交于 2019-11-29 14:49:06
Rizier123

This is because the ternary operator (?:) is left associative so this is how it's getting evaluated:

((1 == 1) ? "one" : (1 == 2)) ? "two" : "three"

So 1 == 1 -> TRUE means that then it's:

"one" ? "two" : "three"

And "one" -> TRUE so the output will be:

two
$chow = 1;
echo ($chow == 1) ? "one" : (($chow == 2) ? "two" : "three");

remember to use brackets when result of operation can be unclear

now output is one

The operator is confused, you need to put brackets around your second codition. use the code below

$chow = 1;
echo ($chow == 1) ? "one" : (($chow == 2) ? "two" : "three"); //returns 1

Hope this helps you

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!