Introduction to using logical operators in php

Source: Internet
Author: User
Tags logical operators

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

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.