65

Hi I just want to know why this code yields (at least for me) an incorrect result.

Well, probably i'm in fault here

$description = 'Paper: ' . ($paperType == 'bond') ? 'Bond' : 'Other';

I was guessing that if paperType equals 'Bond' then description is 'Paper: Bond' and if paperType is not equals to 'Bond' then description is 'Paper: Other'.

But when I run this code the results are description is either 'Bond' or 'Other' and left me wondering where the string 'Paper: ' went???

Cesar
  • 4,076
  • 8
  • 44
  • 68

3 Answers3

123
$description = 'Paper: ' . ($paperType == 'bond' ? 'Bond' : 'Other');

Try adding parentheses so the string is concatenated to a string in the right order.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • Yes, it works, so is a operator precedence problem in my code, shame on me ;~) – Cesar Aug 23 '09 at 00:10
  • 2
    'Paper: ' is being first applied to a boolean ( the result of $paperType == 'bond' ), in other words code is not happening in the right order. – meder omuraliev Aug 23 '09 at 00:18
  • 2
    And to resolve that by specifying parentheses you're basically saying, please return either 'Bond' or 'Other' and then add it to 'Paper: ' INSTEAD OF trying to add 'Paper: ' to true or false. – meder omuraliev Aug 23 '09 at 00:20
  • I get it now, when i concatenate 'Paper: ' to a boolean the result is either 'Paper: ' is the comparison is false or 'Paper: 1' if true, but either way the resulting string evaluate to TRUE, so in my particular case, description will ALWAYS be set to 'Bond', no matter what. Very Thanks. Muchas Gracias! – Cesar Aug 23 '09 at 00:26
  • 3
    Yes, that is correct, and it happens because the "." operator has precedence over the "?:" operator. – João Silva Aug 23 '09 at 00:51
  • When will PHP fix the ternary operator precedence? – Ber May 21 '16 at 00:15
13

It is related with operator precedence. You have to do the following:

$description = 'Paper: ' . (($paperType == 'bond') ? 'Bond' : 'Other');
João Silva
  • 89,303
  • 29
  • 152
  • 158
6

I think everyone gave the solution, I would like to contribute the reason for the unexpected result.

First of all here you can check the origin, and how the operators are evaluated (left, right, associative, etc).

http://php.net/manual/fa/language.operators.precedence.php

Now if we analyze your sentence.

$paperType = 'bond';
$description = 'Paper:'. ($paperType == 'bond') ? 'Bond': 'Other';
  1. We review the table and find that the parentheses are evaluated first, then the '.' (concatenation) is evaluated and at the end the ternary operator '?', therefore we could associate this as follows:
// evaluate the parenthesis ... ($paperType == 'bond')
$description = ('Paper:'. 1)? 'Bond': 'Other';
//result
$description = 'Paper: 1'? 'Bond': 'Other';
  1. We now have the ternary operator, we know that a string is evaluated "true"

// php documentation When converting to boolean, the following values ​​are considered FALSE:

... the empty string, and the string "0"

$description = true? 'Bond': 'Other';
  1. Finally
$description = 'bond';

I hope I have clarified the question. Greetings.

Alistair R
  • 776
  • 9
  • 16
hizmarck
  • 686
  • 9
  • 19