I thought that and in PHP is the same, but it is written for readability and beauty. In fact, I am wrong. There is a deep hole in it! Run the following code: $ bAtrue; $ bBfalse; $ b1 $ bAand $ bB; $ b2 $ bA $ bB; var_dump ($ b1); $ b1truevar_dump ($ b2 ); $ b2false $ bAfalse; $ bB
I thought that and in PHP is the same, but it is written for readability and beauty. In fact, I am wrong. There is a deep hole in it! Run the following code: $ bA = true; $ bB = false; $ b1 = $ bA and $ bB; $ b2 = $ bA $ bB; var_dump ($ b1 ); // $ b1 = truevar_dump ($ b2); // $ b2 = false $ bA = false; $ bB =
I thought thatandAnd&&It is the same, but it is written for readability and beauty. In fact, I am wrong. There is a deep hole in it!
See the following code:
$bA = true;$bB = false;$b1 = $bA and $bB;$b2 = $bA && $bB;var_dump($b1); // $b1 = truevar_dump($b2); // $b2 = false$bA = false;$bB = true;$b3 = $bA or $bB;$b4 = $bA || $bB;var_dump($b3); // $b3 = falsevar_dump($b4); // $b4 = true
Strange,and/&&Andor/||The results are different. Where is the problem?
Let's look at another piece of code!
$bA = true;$bB = false;var_dump($bA and $bB); // falsevar_dump($bA && $bB); // false$bA = false;$bB = true;var_dump($bA or $bB); // truevar_dump($bA || $bB); // true
Even more strange, how is this right? So the problem may occur=Some google and documentation finally found the answer!
Operator priority
Operator priority
Through this table, we can seeand/&&Andor/||The priority of these operators is different.andAndorThe priority is lower=So the above Code is easy to understand, that is, assigning values first and then making anotherandOrorThe result of this operation is not stored, so the final result is incredible.
The following table lists the operators in order of precedence, with the highest-precedence ones at the top. Operators on the same line have equal precedence, in which case associativity decides grouping.
The following table lists the operation priorities. The higher the priority, the same row has the same priority.
| Associativity |
Operator |
Additional information |
| Unassociated |
Clone new |
Clone and new |
| Left |
[ |
Array |
| Left |
** |
Arithmetic |
| Right |
++ -~ (Int) (float) (string) (array) (object) (bool )@ |
Type and auto-increment/auto-Increment |
| Unassociated |
Instanceof |
Type |
| Right |
! |
Logical operation |
| Left |
*/% |
Arithmetic |
| Left |
+ -. |
Arithmetic and string |
| Left |
<> |
Bitwise operation |
| Unassociated |
<=> = |
Comparison |
| Unassociated |
=! ===! ==<> |
Comparison |
| Left |
& |
Bitwise operation and reference |
| Left |
^ |
Bitwise operation |
| Left |
| Bitwise operation |
| Left |
&& |
Logical operation |
| Left |
| Logical operation |
| Left |
? : |
Ternary Condition Selection |
| Right |
= + =-= * =/=. = % = & = |
==<<=>>=> | Assign a value |
| Left |
And |
Logical operation |
| Left |
Xor |
Logical operation |
| Left |
Or |
Logical operation |
| Left |
, |
Many Uses |
Summary
Exercise cautionand,orAndxorLogical operators of, avoid and assign values and&&And||To avoid unnecessary logical errors.
(End)
All Rights Reserved: the source information of the old white is reprinted. < >