On PHP First---PHP bitwise operator "|" and logical Operators "| |" _php Tutorials for problems encountered

Source: Internet
Author: User
in PHP, "|" is a PHP bitwise operator, "| |" As a logical operator
Bit operator Code:
$a = 0;
$b = 0;
if ($a =3 | $b =3) {
$a + +;
$b + +;
}
echo $a. ', '. $b; Output
?>
Compare the code with the following code for the logical operator:
$a = 0;
$b = 0;
if ($a =3 | | $b =3) {
$a + +;
$b + +;
}
echo $a. ', '. $b; Output
?>

In the above two examples, the first example, "$a = 3 | $b =3″ due to "|" Has a higher precedence than the assignment operator, so the order of operations can be written as "$a = (3 | $b =3) ", first $b is assigned a value of 3, and $ A is binary number 0100 | The result assignment of 0100 is still 0100, so $ A at this time is assigned a value of 0100, that is, the decimal 3, the assignment succeeds, returns True, executes if the contents of the block, $a self-addition, $b also self-added, so, $a =4, $b =4


In the second example, the same can be seen as "$a = (3 | | $b = 3)", First 3| | $b = 3 Returns True, "| |" Cause a short circuit, "| |" The previous 3 is already true, "$b =3″ is no longer executed, so at this point $b is still 0, $a is Boolean type true, the assignment succeeds, returns True, executes the contents of the If code block, $a + + is true, $b + + is 1, so $a =1, $b =1


Excerpted from Zdrjlamp

http://www.bkjia.com/PHPjc/478288.html www.bkjia.com true http://www.bkjia.com/PHPjc/478288.html techarticle in PHP, | is the PHP bitwise operator, | | Is the logical operator bit operator code:? php $a = 0; $b = 0; if ($a =3 | $b =3) {$a + +, $b + +;} echo $a.,. $b; Output? Compare the code, the following is the logic ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.