I understand the concept of associativity with regards to mathmatical and assignment operators, but this is going right over my head (and making a whistling noise as it passes).
I understand what is happening (the outcome); I don't understand how it is happening (the process):
Invert the conditions (i.e., !condition) and switch the order of their (if true) / (if false) expressions. You'll end up indenting out, then indenting back in, like this:
echo ( !TRUE ?
echo ( !TRUE ?
equivalent conditions, but it works as expected.
Now, I'm not advocating using deeply nested ternaries everywhere. It's just a thorn in my paw when I can't get something to work that should work, dammit. So, just remember to always put new conditionals first, in the "if true" expression, and never in the "if false".
yeah. both options are kinda kludgy, unfortunately. I don't think it's reasonable/practical to have to put conditions in a specific (unexplained!) order, or manually nest everything in parenthesis, to make it work.
(I'm not advocating deeply-nested conditionals everywhere either, but the above is completely lucid compared to something like c1 ? c2 : c3 ? c4 ? c5 ? v3 : v4 : (c6 ? v5 : v6) : (c7 ? v7 : v4).)
One thing I'll observe at this point - because I see a lot of code where the author has apparently gone to a lot of trouble to avoid it - is that if(condition ? test1 : test2) is legal and doesn't need to be written as if((condition && test1) || (!condition && test2)).
Last edited by Weedpacket; 12-25-2012 at 08:22 PM.