较低优先级运算符会关联非关联较高优先级运算符吗

较低优先级运算符会关联非关联较高优先级运算符吗

本文介绍了较低优先级运算符会关联非关联较高优先级运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> ="和"=="是非关联运算符,当它们彼此相邻时,它们将按优先级进行求值:

">=" and "==" are non-associative operators and when they are next to each other, they get evaluated by precedence:

2 >= 3 == 3

就像:

(2 >= 3) == 3 // ">=" have higher precedence over "=="

但是,如果混合中有关联运算符,甚至是较低优先级的运算符,也会根据关联运算符进行评估:

but if there is an associative operator in the mix, even lower precedence operator, it get evaluated acording to the associative operator:

var_dump($a = 2 >= $b = 3 == 3); // bool(true)
var_dump(2 >= 3 == 3); // bool(false)

这很不正常:

var_dump($a = 2 >= ($b = 3 == 3));
var_dump((2 >= 3) == 3);

我理解正确吗?

推荐答案

PHP始终仅解析定义的方式.并且必须赋予变量赋值更高(隐式)的优先级,因为赋值左侧的必须是变量.不能解析为($a = 2 >= $b) = 3 == 3.它不依赖于关联性.

PHP parses always only the defined ways. And giving variable assignments a higher (implicit) precedence is necessary as on the left of an assignment must be a variable. It's impossible to parse as ($a = 2 >= $b) = 3 == 3. It doesn't depend on the associativity.

看这个例子; &运算符是关联的(而=>不是).

Look at this example; the & operator is associative (and the => isn't).

$b = 2;
$a = 2 >= $b & 2;

在这种情况下,它是从左到右.喜欢:

In this case it is left to right. Like:

var_dump($a = ((2 >= $b) & 2)); // int (0)
var_dump($a = 2 >= $b & 2); // int (0)

比较:

var_dump($a = (2 >= ($b & 2))); // bool (true)

这篇关于较低优先级运算符会关联非关联较高优先级运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:47