Understanding nested PHP ternary operator [duplicate]

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 evaluated left to right. So you are actually getting:

  echo (
    ((($a == 1 ? 'one' : $a == 2)
     ? 'two' : $a == 3) ? 'three' :
    $a == 5) ? 'four' : 'other');

So for $a=2, you get:

  echo (
    ((($a==2) ? 'two' : $a == 3) ? 'three' :
    $a == 5) ? 'four' : 'other');

and then

  echo (
    ((true ? 'two' : $a == 3) ? 'three' :
    $a == 5) ? 'four' : 'other');

and then

  echo (
    ('two' ? 'three' : $a == 5) ? 'four' : 'other');

and then

  echo (
    'three' ? 'four' : 'other');

and so echo 'four'.

Remember that PHP is dynamically typed and treats any non-zero non-null values as TRUE.

Leave a Comment