Php operator priority

Source: Internet
Author: User
Tags php operator
If the operator has the same priority, its combination direction determines whether to evaluate from right to left or from left to right. See the following example. The following table lists operators based on their priorities from high to low. Operators in the same row have the same priority. in this case, their union direction determines the order of value. The operator priority specifies how closely two expressions are bound ". For example, the result of expression 1 + 5*3 is 16 instead of 18 because the priority of the multiplication sign ("*") is higher than that of the plus sign ("+. If necessary, use parentheses to forcibly change the priority. Example: (1 + 5) the value of * 3 is 18.

If the operator has the same priority, its combination direction determines whether to evaluate from right to left or from left to right. See the following example.

The following table lists operators based on their priorities from high to low. Operators in the same row have the same priority. in this case, their union direction determines the order of value.

Operator priority

Additional information of combined direction operators

No clone new clone or new

Left [array ()

Right ++ --~ (Int) (float) (string) (array) (object) (bool) @ Type and increment/decrease

No instanceof type

Right! Logical operators

Left */% arithmetic operator

Left +-. arithmetic operators and string operators

Left <> bitwise operator

None =! ===! ==<> Comparison operators

Left & bit operators and references

Left ^ bit operator

Left | bitwise operator

Left & logical operators

Left | logical operator

Left? : Ternary operator

Right = + =-= * =/=. =%=<<=>==> value assignment operator

Left and logical operators

Left xor logical operator

Left or logical operators

Left, used in multiple places

For operators with the same priority, the left union direction means that the value is calculated from left to right, and the right union direction is vice versa. For operators with the same priority without the direction of integration, this operator may not be able to combine with itself. For example, in PHP, 1 <2> 1 is an invalid statement, while 1 <= 1 = 1 is not. Because the priority of the T_IS_EQUAL operator is lower than that of the T_IS_SMALLER_OR_EQUAL operator.

Example #1 integration direction

  $a = 5, $b = 5// mixing ++ and + produces undefined behavior$a = 1;echo ++$a + $a++; // may print 4 or 5?>

Use parentheses to enhance the readability of the code even if it is not strictly required.

Note:

Although = has a lower priority than most other operators, PHP still allows expressions similar to the following: if (! $ A = foo (). In this example, the return value of foo () is assigned to $.

Example:

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.