1

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?

zhenguoli
  • 2,268
  • 1
  • 14
  • 31

1 Answers1

2

Ignore the non-?: operators here by substitution as they have a higher precedence.

3<8 ? (9<6 ? 7 : 5) : 2>0 ? 4 : 1

then becomes

x ? (y ? 7 : 5) : z ? 4 : 1

and when applying leftward associativity of ?: the result is

(x ? (y ? 7 : 5) : z) ? 4 : 1

Substitute back as desired.

As for

..what does non-associative associativity mean in PHP?

Non-associativity describes infix operators that "don't chain" and "generally means that syntactically, there is a special rule for sequences of these operations, and semantically the behavior is different" - including syntax errors! Take the following:

1 < b < 2

If it < was leftward it would be (1 < b) < 2 (or 1 < (b < 2) for rightward). However, such construct does not make sense in PHP and thus the form 1 < b < 2 merely results in a syntax error.

While it is valid syntax, although semantically odd, to apply parenthesis manually as done above, the non-associative nature of the operators means PHP will not even try to parse it as such.

On the other hand, many operators like + and * "do chain" and thus have an associativity.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • @zhenguoli Yes. – user2864740 Jul 17 '17 at 03:29
  • But I wonder what the non-associative associativity is in PHP. Can you explain that? It is strange. – zhenguoli Jul 17 '17 at 03:31
  • If it won't chain, then how PHP determine the way of evaluation? – zhenguoli Jul 17 '17 at 03:35
  • @zhenguoli It does not parse it, and thus cannot evaluate it. – user2864740 Jul 17 '17 at 03:37
  • Thanks. I see, it will complain syntax error for that case. But in C++, it won't. – zhenguoli Jul 17 '17 at 03:39
  • @zhenguoli You're welcome. In Python it'd "work as a non-programmer might expect" :D – user2864740 Jul 17 '17 at 03:42
  • Thanks for your reminder. Yes, Python thinks more like humans. – zhenguoli Jul 17 '17 at 03:44
  • As for your statement: "Ignore the non-?: operators here by substitution as they have a lower precedence." What does `they` refer to? Does `they` refer to "non-?:" operators? But "non-?:" operators have higher precedence. – zhenguoli Jul 17 '17 at 03:53
  • @zhenguoli Oops. That should have been *higher* (corrected now, I shouldn't answer so late at night >_<). Anyway, since all the *other* operators in *this question* had a higher precedence, they formed expression groups couldn't be broken up by the ?:, and can thus be substituted as with a placeholder expression replacement - this was made explicit when added parenthesis manually. The idea behind the start of this answer with the use of substitutions is that parsing questions can often be looked at as layers that are applied as a series of steps. – user2864740 Jul 17 '17 at 15:07
  • @zhenguoli There are, of course, "gotchas" with irregular grammar rules and forms that *look* like one things (such as an expression) although they are not really - eg. https://stackoverflow.com/questions/32672016 – user2864740 Jul 17 '17 at 15:12
  • Thank u for your help and your patience. It doesn't matter. – zhenguoli Jul 18 '17 at 00:39