This question already has an answer here:
$chow = 3;
echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three";
output: three
$chow = 1;
echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three";
output: two
Can anyone explain why the output is "two" when $chow = 1 instead of "one"?
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
Hope this helps you
This is because the ternary operator (
?:
) is left associative so this is how it's getting evaluated:So
1 == 1
->TRUE
means that then it's:And
"one"
->TRUE
so the output will be: