2
<?php echo true?'what':true?'will':'print?';?>  

Above code outputs will.
I am not able to get logic behind it. Can anyone explain it.

Thanks in advance.

Ritesh
  • 113
  • 1
  • 1
  • 9
  • You should work with braces: `echo true?'what':(true?'will':'print?');` this will output `what`. Probably the second if overrides the first if if there are no braces. – TiMESPLiNTER Sep 25 '14 at 11:10
  • see http://stackoverflow.com/questions/889373/quick-php-syntax-question – EvilEpidemic Sep 25 '14 at 11:16
  • which one is true? according to @TiMESPLiNTER, ternary expressions get interpreted from right to left. and according to `Justinas`, ternary expressions are evaluated from left to right – DS9 Sep 25 '14 at 11:23
  • Ternary expressions are evaluated from left to right: http://php.net/manual/en/language.operators.comparison.php – jvdhooft Sep 25 '14 at 11:28
  • @DS9 Sorry wrote it the wrong way but did the examples the right way. I fixed it in my answer. – TiMESPLiNTER Sep 25 '14 at 11:50
  • possible duplicate of [Why is the output of \`echo true ? 'a' : true ? 'b' : 'c';\` 'b'?](http://stackoverflow.com/questions/14751225/why-is-the-output-of-echo-true-a-true-b-c-b) – RandomSeed Sep 26 '14 at 12:42

4 Answers4

2

You should work with braces:

echo true?'what':(true?'will':'print?'); 

this will output what. The second if overrides the first if if there are no braces. Because ternary expressions get interpreted from left to right. So without any braces set by you PHPs interpreter will interpret your statement as:

echo (true?'what':true)?'will':'print?'; 

According to PHP.net you should avoid stacking ternary expressions:

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:

TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
2

From documentation:

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:

Example #4 Non-obvious Ternary Behaviour

// 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.

Justinas
  • 41,402
  • 5
  • 66
  • 96
1

The ternary operator in PHP is left-associative. Your code evaluates like this:

echo (true ? 'what' : true) ? 'will' : 'print?';

This is equivalent to:

echo (true) ? 'will' : 'print?';

And thus, the result is 'will'. You should use the following:

echo true ? 'what' : (true ? 'will' : 'print?');

A related post can be found here: Why is the output of `echo true ? 'a' : true ? 'b' : 'c';` 'b'?

Community
  • 1
  • 1
jvdhooft
  • 657
  • 1
  • 12
  • 33
1

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: (as given in document)

Example:

`

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.

`

Kumar Kailash
  • 1,385
  • 1
  • 10
  • 19