Understanding nested PHP ternary operator
I dont understand how that output (" four ") comes? $a = 2; echo $a == 1 ? 'one' : $a == 2 ? 'two' : $a == 3 ? 'three' : $a == 5 ? 'four' : 'other' ; // prints 'four' I don't understand why " four " gets printed. You need to bracket the ternary conditionals: <?php for ($a=0; $a < 7; $a++) { echo ( $a == 1 ? 'one' : ($a == 2 ? 'two' : ($a == 3 ? 'three' : ($a == 5 ? 'four' : 'other')))); echo "\n"; // prints 'four' } exit; ?> returns: other one two three other four other as you'd expect. See the note at the bottom of "Ternary operators" at PHP Ternary operator help . The expressions are being