PHP's first bullet --- php bit operators "|" and logical operators "|"

Source: Internet
Author: User

In php, "|" is a php bit operator, and "|" is a logical operator.
Bitwise operator 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:
<? 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", because "|" has 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" is not executed, so $ B is still 0 at this time, $ 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


From zdrjlamp

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.