I got this problem. I don’t know if I’m just too tired or if there is something not working.
My code is:
$sX = ($nr == 1 or $nr == 4 or $nr == 7 ? "0" : ($nr == 2 or $nr == 5 or $nr == 8 ? "200" : "400"));
$sY = ($nr == 1 or $nr == 2 or $nr == 3 ? "0" : ($nr == 4 or $nr == 5 or $nr == 6 ? "200" : "400"));
$nr is an integer in the range [1..9] (it’s in a loop). Just right one row below this statement, I output this two vars. And then both of them are "1" or null. Why? I just can’t see it 🙁
Thanks
Flo
It’s because the ternary operator has a higher operator precedence than logical
or. Add parentheses around all of yourx or y or zparts. In other words, you might as well have written that expression as:Same for the second one.
Reference: http://php.net/language.operators.precedence