Wednesday, 15 September 2010

php - Ternary operator and string concatenation quirk? -


Hello, I want to know why this code generates (at least for me) wrong results

OK, maybe I'm making a mistake here

  $ description = 'paper:' ($ papertype == 'bond')? 'Bond': 'Other'; I was guessing that if the papertepe is equal to 'Bond' then the details are 'Paper: Bond' and if the papertype is not equal to 'Bond' then the description is 'Paper: Other'.  
P>

But when I run this code, the result is described as' Bond 'or' Other 'and I am left out thinking' where did the string 'paper' go? Was ???

  $ description = 'paper:' ($ papertepe == 'bond'? 'Bond ': 'other');  

Try adding brackets so that the string is added to a string in the correct order.


No comments:

Post a Comment