Note:
Priority :! >&>||> And> xor> or
Note: Even though! Higher than =, PHP still allows expressions similar to the following: if (! $ A = foo (). In this example, the output of foo () is assigned to $.
Let's take a look at the manual and agree with the following sentence: using parentheses can enhance the readability of the code.
Example |
Name |
Result |
$ A and $ B |
And (logical And) |
TRUEIf both $ a and $ B areTRUE. |
$ A or $ B |
Or (logical Or) |
TRUEIf either $ a or $ B isTRUE. |
$ A xor $ B |
Xor (logical exclusive or) |
TRUEIf either $ a or $ B isTRUEBut not at the same time. |
! $ |
Not (logical non) |
TRUEIf $ a is notTRUE. |
$ A & $ B |
And (logical And) |
TRUEIf both $ a and $ B areTRUE. |
$ A | $ B |
Or (logical Or) |
TRUEIf either $ a or $ B isTRUE. |
Example
In php, "|" is a php bit operator, and "|" is a logical operator.
Bitwise operator code:
The code is as follows: |
Copy code |
<? Php $ A = 0; $ B = 0; If ($ a = 3 | $ B = 3 ){ $ A ++; $ B ++; } Echo $ a. ','. $ B; // output 4. 4 ?>
|
The following code compares the code with the logic operator:
The code is as follows: |
Copy code |
<? Php $ A = 0; $ B = 0; If ($ a = 3 | $ B = 3 ){ $ A ++; $ B ++; } Echo $ a. ','. $ B; // output 1, 1 ?> |
In the first example, "$ a = 3 | $ B = 3 & Prime;" is given a higher priority than the value assignment operator, therefore, the operation order can be written as "$ a = (3 | $ B = 3)". First, $ B is assigned as 3, and $ a is assigned a value for the result of 0100 | 0100, $ a is still 0100. Therefore, $ a is assigned a value of 0100, that is, 3 in decimal format. if the value is successfully assigned, true is returned. if the content in the code block is executed, $ a is automatically added, $ B is also self-added, so $ a = 4, $ B = 4
In the second example, it can also be seen as "$ a = (3 | $ B = 3)". First, 3 | $ B = 3 returns true, "|" causes short circuit, "|" the first 3 is true, "$ B = 3 & Prime; no longer executed, so $ B is still 0, $ a is a Boolean value of true. if the value is successfully assigned, true is returned. if the content in the code block is executed, $ a ++ is also true, and $ B ++ is 1. Therefore, $ a = 1, $ B = 1