15

Why is this printing 2?

echo true ? 1 : true ? 2 : 3;

With my understanding, it should print 1.

Why is it not working as expected?

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Riz
  • 703
  • 1
  • 7
  • 16
  • 4
    There's never ever a good reason to nest ternary operators... and the manual explicitly warns you about this – Mark Baker Jan 08 '13 at 11:58
  • 2
    http://php.net/manual/en/language.operators.comparison.php - `Note: It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious` - ternary expressions are left associative. – DaveRandom Jan 08 '13 at 12:00
  • 1
    if you *must* nest your ternary operatory, then use brackets around them. In fact, I'd recommend using brackets around ternaries anyway; it makes them a lot more readable even if you're not nesting them. – SDC Jan 08 '13 at 12:00

6 Answers6

27

Because what you've written is the same as:

echo (true ? 1 : true) ? 2 : 3;

and as you know 1 is evaluated to true.

What you expect is:

echo (true) ? 1 : (true ? 2 : 3);

So always use braces to avoid such confusions.

As was already written, ternary expressions are left associative in PHP. This means that at first will be executed the first one from the left, then the second and so on.

doppelgreener
  • 4,809
  • 10
  • 46
  • 63
Leri
  • 12,367
  • 7
  • 43
  • 60
3

Use parentheses when in doubt.

The ternary operator in PHP is left-associative in contrast to other languages and does not work as expected.

Adder
  • 5,708
  • 1
  • 28
  • 56
3

Separate second ternary clause with parentheses.

echo true ? 1 : (true ? 2 : 3);
Lauris
  • 1,085
  • 10
  • 16
2

from the docs

Example #3 Non-obvious Ternary Behaviour
<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');

// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right

// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');

// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
0

In your case, you should consider the priority of executing statements.

Use following code:

echo true ? 1 : (true ? 2 : 3);

For example,

$a = 2;
$b = 1;
$c = 0;

$result1 = $a ** $b * $c;
// is not equal
$result2 = $a ** ($b * $c);

Are you have used parentheses in expressions in mathematics? - Then, that the result, depending on the execution priority, is not the same. In your case, ternary operators are written without priorities. Let the interpreter understand in what order to perform operations using parentheses.

Vitaly Vesyolko
  • 558
  • 5
  • 22
0

Late, but a nice example for being carefull from now:

$x = 99;
print ($x === 1) ? 1
    : ($x === 2) ? 2
    : ($x === 3) ? 3
    : ($x === 4) ? 4
    : ($x === 5) ? 5
    : ($x === 99) ? 'found'
    : ($x === 6) ? 6
    : 'not found';
// prints out: 6

Result of PHP 7.3.1 (Windows Commandline). I can not understand, why they changed it, because the code becomes really unreadable, if I try to change this.

Sven
  • 524
  • 4
  • 10