I was asked to perform this operation of ternary operator use:
$test='one';
echo $test == 'one' ? 'one' : $test == 'two' ? 'two' : 'three';
Which prints two (checked using php).
I am still not sure about the logic for this. Please, can anybody tell me the logic for this.
Well, the ? and : have equal precedence, so PHP will parse left to right evaluating each bit in turn:
First
$test == 'one'returns true, so the first parens have value ‘one’. Now the second ternary is evaluated like this:‘one’ is true (a non-empty string), so ‘two’ is the final result.