How to understand PHP operator precedence?
伊谢尔伦
伊谢尔伦 2017-05-16 13:04:40
0
2
550
<?php
$a = 1;
$b = 2;
$c = 3;
$d = 4;
$a = $b !== $c && $d = $b == $c;
var_dump($a,$d);exit;

What I understand here is: First, because !== has a higher priority than &&, first judge $b !== $c (is true) and $b == $c* (is false), and then perform && Judgment, and for &&, execute the left side first, and then execute the right side based on the result. Then the left side is $a = true, and then the right side is $d = false, but why is printing $a false? Then I understand that this is actually:

$a = (true && ($d = false));

I don’t understand this. If && does not refer to the expression on the left, but the value of $b !== $c on the left, then why can $d = be run on the right? False, how does this combination work...
I checked a lot of information and asked several colleagues. They all said that it is difficult to explain and cannot be used in daily development. But as a rookie, I have obsessive-compulsive disorder. I really want to figure it out, please give me some advice on how to understand and judge the operator priority and execution issues of PHP, because in fact there are quite a lot of this way of writing in open source code.
In fact, it is similar to $a = $b && $b = $c;Finally $a is true, but $b is the value of $c. (Assume that the three variables abc are 1, 2, and 3 above).

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
伊谢尔伦

For logical operators, you can look at it this way. The expressions on both sides of each logical operator will be combined into a whole.
For $b !== $c && $d = $b == $c, for this expression, two combinations will be generated: $b !== $c && $d = $b == $c,对于这个表达式,将会生成两个组合:
($b !== $c) && ($d = $b == $c)
组合1: ($b !== $c)
组合2: ($d = $b == $c)
所以先执行组合1($b !== $c),结果是true,根据短路运算,需要执行组合2($d = $b == $c),对于这个组合,里面又由于==运算符优先级高,执行==,在赋值,也就是false. 所以最终组合1的结果是true,组合2的结果是false,所以$a=false.
这里解释了可以执行右边的赋值表达式,而不执行左边的赋值表达式,因为左边的赋值表达式优先级低,在执行它之前,需要执行完!==, 再执行&&,之后接着执行==,再执行右边的=,再执行左边的=($b !== $c) && ($d = $b == $c)combination 1: ($b !== $c)

Combination 2: ($d = $b == $c)🎜So execute combination 1( first $b !== $c), the result is true, according to the short-circuit operation, combination 2 ($d = $b == $c) needs to be executed , for this combination, because the == operator has a high priority, == is executed before assigning, which is false. So in the end The result of combination 1 is true, and the result of combination 2 is false, so $a=false. 🎜🎜Here it is explained that the right side can be executed Assignment expression instead of executing the assignment expression on the left, because the assignment expression on the left has a low priority. Before executing it, !== needs to be executed, and then && code>, then execute ==, then execute = on the right, and then execute = on the left🎜.🎜
淡淡烟草味

I’m here to complain. I haven’t looked at this line you wrote $a = $b !== $c && $d = $b == $c; carefully, I copied and pasted it, haha, 666..

Big brother:
1. Such code is in books;
2. Such code projects rarely exist, almost 0
3. Even if there are similar requirements, () will be used to control it to achieve readability The purpose of making the model stronger
4. Such a requirement can be simply understood, but the usability is not strong.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template