Operator Precedence in PHP

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators

The operator precedence specifies how tightly the two expressions are bound. For example, the result of the expression 1 + 5 * 3 is that it is because multiplication sign ("*") has a higher priority than the plus sign ("+"). You can use parentheses to force a change of precedence if necessary. For example:(1 + 5) * 3 is the value of.

If the operator has the same precedence, the union direction of the operator determines how it is to be calculated. For example, "-" is leftist, then 1-2-3 is equivalent to (1-2)-3 and the result is -4. On the other hand, "=" is right-linked, so $a = $b = $c equals $a = ($b = $c).

Operators that do not combine the same precedence can not be used together, for example, 1 < 2 > 1 is not valid in PHP. But on the other hand the expression 1 <= 1 = = 1 is legal, because = = priority is lower than <=.

The use of parentheses, even if not necessary, by pairing the parentheses to clearly indicate the order of operations, rather than by operator precedence and binding to determine, usually can increase the readability of the code.

The following table lists the operators by priority from high to low. Operators in the same row have the same precedence, at which point their binding direction determines the order of evaluation.

Combination Direction operator Additional Information
No Clone new Clone and New
Left [ Array ()
Right ** Arithmetic operators
Right ++ -- ~ (int) (float) (String) (Array) (object) (BOOL) @ Type and increment/decrement
No instanceof Type
Right ! logical operators
Left * / % Arithmetic operators
Left + - . Arithmetic operators and string operators
Left << >> Bitwise operators
No < <= > >= Comparison operators
No == != === !== <> <=> Comparison operators
Left & Bitwise operators and references
Left ^ Bitwise operators
Left | Bitwise operators
Left && logical operators
Left || logical operators
Left ?? Comparison operators
Left ? : Ternary
Right = += -= *= **= /= .= %= &= |= ^= <<= >>= Assignment operators
Left and logical operators
Left Xor logical operators
Left Or logical operators

Operator Precedence in PHP

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.