例如,变量赋值到 if 语句中

Variable assignment into if statement for example

实际上我对运算符优先级有点困惑。

Here is an example that doesn't match with the php official website

 function getValue($key) {
      $array = ['un' => 1, 'six' => 6];

      if (array_key_exists($key, $array)) {
           return $array[$key];
      }

      return null;
 }

 var_dump(null === $t1=getValue('un'));
 var_dump(null === $t2=getValue('blablablaaaaaa'));
 // And now I switch
 var_dump($t3=getValue('un') ===  null);
 var_dump($t4=getValue('blablablaaaaaa') === null);

输出

 bool(false)
 bool(true)
 // After the switch
 bool(false)
 bool(true)

这不是我对前两个输出的期望,因为比较的优先级高于赋值。所以 php 应该尝试比较 null === $t1,或者 $t1 还没有被声明,所以警告或错误或任何应该被提出的。或者那没有发生。 您是否注意到 PHP 有时在比较之前处理赋值,尽管比较优先级更高,因此应该始终在赋值之前执行?。有什么解释吗?

我的第二个问题是:是否应该通过始终拆分这种表达式来避免这种情况?

更新

N.B

 var_dump($t1, $t2, $t3, $t4); 
 // OUTPUT

 int(1)
 NULL
 // After the switch
 bool(false)
 bool(true)

===非关联

Non-associative operators are operators that have no defined behavior when used in sequence in an expression.

并且在docs

= has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.

所以可以假设PHP(在任何表达式中)会先将函数的return值赋值给变量,然后再进行比较。