As for the expression:
3<8 ? (9<6 ? 7 : 5) : 2>0 ? 4 : 1
I have referred to the PHP manual. And I have found that the ternary operator in PHP is left-associative. Moreover, I find that the associativity of relational operator such as >
, <=
is non-associative, which sounds strange to me.
According to the precedence and associativity of ternary operator in PHP, it can be expressed as groups according to other's opinion:
(3<8 ? (9<6 ? 7 : 5) : 2)>0 ? 4 : 1
But as for me, I group the expression like this:
((3<8) ? (9<6?7:5) : (2>0)) ? 4 : 1
So which one is right? The main difference is that the sub-expression 2>0
is split or not.
And I wonder what the non-associative associativity is in PHP. I find that it sounds strange and haven't seen it in other languages like C, Java, C++.
There is already a question about the meaning of PHP non-associative:
do lower precedence operators associate non-associative higher precedence operators?
But I can't figure out what does non-associative associativity mean in PHP?