表达式“3<8 ? (9<6 ? 7 : 5) : 2>0 ? 4 : 1”的实群是什么以及PHP中非结合的含义?

What is the real groups of the expression "3<8 ? (9<6 ? 7 : 5) : 2>0 ? 4 : 1" and the meaning of non-associative in PHP?

至于表达:

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

我已经提到了PHP manual。而且我发现 PHP 中的三元运算符是左关联的。 另外,我发现><=等关系运算符的结合性是非结合性的,这让我觉得很奇怪

根据PHP中三元运算符的优先级和结合性,可以按照其他人的意见表示为组:

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

但对于我来说,我将表达式分组如下:

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

那么哪一个是正确的?主要区别在于子表达式 2>0 是否拆分。

我想知道 PHP 中的非关联关联性是什么。我发现它听起来很奇怪,并且没有在其他语言如 C、Java、C++ 中看到它。

关于PHP non-associative的意思已经有提问了: do lower precedence operators associate non-associative higher precedence operators?
但我无法弄清楚 PHP?

中的非关联关联性是什么意思

忽略非 ?: 运算符,因为它们具有 更高 优先级。

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

然后变成

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

当应用 ? 的左结合律时:结果是

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

根据需要替换回来。

至于

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

非关联性描述了 "don't chain" 和 "generally means that syntactically, there is a special rule for sequences of these operations, and semantically the behavior is different" 的中缀运算符 - 包括语法错误!采取以下措施:

1 < b < 2

如果它 < 向左,它将是 (1 < b) < 2 (或 1 < (b < 2) 向右)。但是,这种结构 在 PHP 中没有意义,因此 1 < b < 2 的形式只会导致语法错误。

尽管语义上很奇怪,但像上面那样手动应用括号是有效的语法,运算符的非关联性质意味着 PHP 甚至不会尝试这样解析它。

另一方面,许多运算符如 +*do chain” 因此具有关联性。