PHP first bullet --- php bit operator "|" and logical operator "|" problems _ PHP Tutorial

Source: Internet
Author: User
The first bullet in PHP is the php bit operator "|" and the logical operator "|. In php, | is a php bit operator, | is the code of the logical operator bit operator :? Php $ a0; $ b0; if ($ a3 | $ b3) {$ a ++; $ B ++;} echo $ a.,. $ B; output 4, 4? Compare the code. The following is the logic in php. "|" is the php bit operator, and "|" is the logical operator.
Bitwise operator code:
$ 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:
$ 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

Operator, | it is a php bit operator, | it is the logic operator bit operator code :? Php $ a = 0; $ B = 0; if ($ a = 3 | $ B = 3) {$ a ++; $ B ++ ;} echo $ .,. $ B; // output 4? 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.