Home >Backend Development >PHP Tutorial >A brief discussion on the first issue of PHP - the problems encountered with the php bit operator '|' and the logical operator '||'_PHP Tutorial
In php, "|" is a php bit operator, and "||" is a logical operator
Bit operator code:
$a=0;
$b=0;
if($a=3 | $b=3){
$a++;
$b++;
}
echo $a.','.$b; //Output 4,4
?>
Compare the code, the following is the code of the logical operator:
$a=0;
$b=0;
if($a=3 || $b=3){
$a++;
$b++;
}
echo $a.','.$b; //Output 1,1
?>
In the above two examples, in the first example, "$a=3 | $b=3", since the priority of "|" is higher than the assignment operator, the order of operations can be written as "$a =(3 | $b=3)", first $b is assigned the value 3, and $a is assigned the value of the binary number 0100 | 0100, which is still 0100, so $a is assigned the value 0100 at this time, which is decimal 3. The assignment is successful, true is returned, and the content in the if code block is executed. $a is incremented, and $b is also incremented. Therefore, $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 a short circuit, and the "||" before 3 is already true, "$b=3" is no longer executed, so $b is still 0 at this time, $a is true of Boolean type, the assignment is successful, true is returned, the content in the if code block is executed, and $a++ is returned is true, $b++ is 1, so, $a=1,$b=1