2

Possible Duplicate:
PHP ternary operator not working as expected

I dont know what is wrong with my code? My PHP Version is 5.4.7.

$b = 'a';
$c = 'd';
echo $b == 'a' ? 2: $c == 'a' ? 1 : 0; 

output 1

right answer should be 2.....

Thank you very much for your advice.

1

1 Answer 1

8

You need to add some parenthesis.

$b = 'a';
$c = 'd';
echo ($b == 'a') ? 2 : ($c == 'a' ? 1 : 0);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! May I ask why this will not work in php??
I mean the way I got wrong, it should be right in js
The best answer to this was given by @DCoder. I recommend reading his link.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.