I have this bit of PHP code:
echo true ? 'a' : true ? 'b' : 'c';
The output of this is:
b
But the output I expected was:
a
I have this bit of PHP code:
echo true ? 'a' : true ? 'b' : 'c';
The output of this is:
b
But the output I expected was:
a
the ternary operator in php is left-associative.
You need to use
echo true ? 'a' : (true ? 'b' : 'c');
Because your code evaluates like this:
echo (true ? 'a' : true) ? 'b' : 'c';
it equivalent to:
echo (true) ? 'b' : 'c';
Then the result is 'b'