Php operator priority order explanation

Source: Internet
Author: User
Tags php operator
When talking about PHP logical operators, we mentioned the priority of PHP operators. The so-called operator priority refers to which operator is first calculated and then calculated in the expression, the result of expression 1 + 5*3 mentioned the priority of the PHP operator when we talked about the PHP logical operator, it refers to the operator in the expression that is first calculated and then calculated. it is like the result of expression 1 + 5*3.

It is 16 instead of 18 because the multiplication ("*") has a higher priority than the plus sign ("+. If necessary, use parentheses to forcibly change the priority. Example: (1 + 5) the value of * 3 is 18.

The PHP operator follows the rule in the operation: the operation with a higher priority is executed first, and the operation with a lower priority is executed later. if the operation has the same priority, the operation is executed from left to right. for example, "-" is left join, so 1-2-3 is equivalent to (1-

2)-3 and the result is-4. on the other hand, "=" is right join, so $ a = $ B = $ c is equivalent to $ a = ($ B = $ c ).

Operators in parentheses are executed first. even if they are not necessary, parentheses are used to clearly indicate the order of operation, rather than operator priority and associativity, this improves code readability.

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

Integration direction Operator Additional information
None Clone new Clone and new <
Left [ Array ()
Right ** Arithmetic operators
Right ++ -- ~ (Int) (Float) (String) (Array) (Object) (Bool) @ Type and increment/decrease
None Instanceof Type
Right ! Logical operators
Left * / % Arithmetic operators
Left + - . Arithmetic operators and string operators
Left < > Bitwise operators
None < <= > > = Comparison Operators
None = ! = === ! = <> <=> Comparison Operators
Left & Bitwise operators and references <
Left ^ Bitwise operators
Left | Bitwise operators
Left && Logical operators
Left | Logical operators
Left ?? Comparison Operators
Left ? : Ternary operators
Right = + = -= * = ** = /= . = % = & = | = ^ = <= >>= Value assignment operator
Left And Logical operators
Left Xor Logical operators
Left Or Logical operators

It is unrealistic and unnecessary to remember so many priorities. if the written expressions are complex and contain many operators, use the brackets as follows:

 

This reduces the possibility of logical errors.

Sometimes parentheses can enhance the readability of the code. For example:

 $ A = 5, $ B = 5?>

The above is a detailed explanation of the php operator priority order. For more information, see other related articles in the first PHP community!

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.