I’m struggling to understand why the following code is echoing out ‘FOO2’ when i’m expecting ‘FOO1’
$tmp = 'foo1';
echo $tmp == 'foo1' ? 'FOO1' : $tmp == 'foo2' ? 'FOO2' : 'NO FOO';
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
basically PHP breaks this down to:
the part in parentheses will return
FOO1which evaluates toTRUEso the second conditional statement essentially isTRUE ? 'FOO2' : 'NO FOO';– which in turn always evaluates to'FOO2'Note: This is different from C ternary operator associativity