PHP operator Precedence

Source: Internet
Author: User
Tags php operator

When we talked about the PHP logical operators, we mentioned the precedence of the PHP operators, the so-called operator precedence, which is the calculation of which operation Mr. Foo in the expression, which is calculated as if the result of the expression 1 + 5 * 3 is 16 instead of 18 because multiplication sign ("*") has precedence over the plus sign ("+ ") high. If necessary, you can use parentheses to force a change in precedence. For example: (1 + 5) * 3 has a value of 18. The rules that are followed in the operation of the PHP operators are: High priority operations are performed first, followed by low priority operations, and if the same priority is performed in left-to-right order, 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). The operators in 1486188938564607.gif parentheses are executed first, 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, can improve the readability of the code. The 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. Append information with direction operator no clone Newclone and new< left [array () Right * * arithmetic operator right + +-~ (int) (float) (string) (array) (object) (BOOL) @ Type and increment/decrement no instanceof type right! Logical operator Left */% arithmetic operator left +-. Arithmetic operator and string operator left << >> bitwise operator None < <= > >= comparison operator None = = = = =!== & Lt;> <=> comparison operator left & bitwise operator and Reference < left ^ bitwise operator LEFT | bitwise operator LEFT && logical operator left | | Logical operator left?? Comparison operator left? : ternary operator right = + = = *= **=/=. =%= &= |= ^= <<= >>= assignment operator left and logical operator left XOR logical operator left or logical operator so many priority levels, if you want to remember very clearly, is not too realistic, nor It is necessary to write expressions that are complex and contain more run-in characters, and do not use parentheses, like this: <?php$a and (($b! = $c) or (5* (50-$d)); > SoYou can reduce the likelihood that a logic error may occur sometimes using parentheses can enhance the readability of your code. For example, the following: <?php$a = 3 * 3 5; (3 * 3)% 5 = 4//ternary run combined $ A = true? 0:true? 1:2; (true? 0:true)? 1:2 = 2 $a = 1; $b = 2; $a = $b + = 3; $a = ($b + = 3), $a = 5, $b = 5?> above is the details of the order of precedence of PHP operators, more attention to the PHP Chinese web other related articles!

PHP operator Precedence

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.