Please consider the follwing code construction:
condition ? code_if_true :
condition2 ? code_if_true2 :
code_if_false;
This doesn’t work for PHP whereas it does for JavaScript.
Is there a way to get this working for PHP?
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.
In PHP, the conditional operator is left-associative[PHP.net], compared to virtually all other languages where it is right-associative.
That’s why you need to use parentheses to control the order of evaluation1:
1The order in which which operators are resolved, not when operands are evaluated. The latter is basically undefined[PHP.net]