A pitfall of the logical operators and/or and |/& in PHP

Source: Internet
Author: User
I thought that and in PHP is the same, but it is written for readability and beauty. In fact, I am wrong. There is a deep hole in it! Run the following code: $ bAtrue; $ bBfalse; $ b1 $ bAand $ bB; $ b2 $ bA $ bB; var_dump ($ b1); $ b1truevar_dump ($ b2 ); $ b2false $ bAfalse; $ bB

I thought that and in PHP is the same, but it is written for readability and beauty. In fact, I am wrong. There is a deep hole in it! Run the following code: $ bA = true; $ bB = false; $ b1 = $ bA and $ bB; $ b2 = $ bA $ bB; var_dump ($ b1 ); // $ b1 = truevar_dump ($ b2); // $ b2 = false $ bA = false; $ bB =

I thought thatandAnd&&It is the same, but it is written for readability and beauty. In fact, I am wrong. There is a deep hole in it!

See the following code:

$bA = true;$bB = false;$b1 = $bA and $bB;$b2 = $bA && $bB;var_dump($b1); // $b1 = truevar_dump($b2); // $b2 = false$bA = false;$bB = true;$b3 = $bA or $bB;$b4 = $bA || $bB;var_dump($b3); // $b3 = falsevar_dump($b4); // $b4 = true

Strange,and/&&Andor/||The results are different. Where is the problem?

Let's look at another piece of code!

$bA = true;$bB = false;var_dump($bA and $bB); // falsevar_dump($bA && $bB); // false$bA = false;$bB = true;var_dump($bA or $bB); // truevar_dump($bA || $bB); // true

Even more strange, how is this right? So the problem may occur=Some google and documentation finally found the answer!

Operator priority

Operator priority

Through this table, we can seeand/&&Andor/||The priority of these operators is different.andAndorThe priority is lower=So the above Code is easy to understand, that is, assigning values first and then making anotherandOrorThe result of this operation is not stored, so the final result is incredible.

The following table lists the operators in order of precedence, with the highest-precedence ones at the top. Operators on the same line have equal precedence, in which case associativity decides grouping.

The following table lists the operation priorities. The higher the priority, the same row has the same priority.

Associativity Operator Additional information
Unassociated Clone new Clone and new
Left [ Array
Left ** Arithmetic
Right ++ -~ (Int) (float) (string) (array) (object) (bool )@ Type and auto-increment/auto-Increment
Unassociated Instanceof Type
Right ! Logical operation
Left */% Arithmetic
Left + -. Arithmetic and string
Left <> Bitwise operation
Unassociated <=> = Comparison
Unassociated =! ===! ==<> Comparison
Left & Bitwise operation and reference
Left ^ Bitwise operation
Left | Bitwise operation
Left && Logical operation
Left | Logical operation
Left ? : Ternary Condition Selection
Right = + =-= * =/=. = % = & = ==<<=>>=> | Assign a value
Left And Logical operation
Left Xor Logical operation
Left Or Logical operation
Left , Many Uses
Summary

Exercise cautionand,orAndxorLogical operators of, avoid and assign values and&&And||To avoid unnecessary logical errors.

(End)

All Rights Reserved: the source information of the old white is reprinted. < >

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.