Differences in operator precedence between PHP and JavaScript

Source: Internet
Author: User
The two priorities are the same, and the comparison (comparision) and Assignment (Assignment) operations are slightly different. The comparison operator has a, <, >=, and so on, the assignment operator has =, + =, *=, and so on.

The comparison operator in JS has a higher precedence than the assignment operator. So foo = 1 < 2 result Foo = false;

In PHP, in turn, the assignment operator takes precedence over the comparison operator. So foo = 1 < 2 result foo = 1, the expression is false. To achieve the same result as above, enclose the parentheses

Foo = (1 < 2)

Attached: full operator precedence for PHP and JavaScript

Js

The following table is ordered from highest (+) to lowest (0) precedence.

Precedence Operator Type associativity Individual Operators
19 Grouping N/A ( … )
18 Member Access Left-to-right … . …
Computed Member Access Left-to-right … [ … ]
New (with argument list) N/A new … ( … )
17 Function Call Left-to-right … ( … )
New (without argument list) Right-to-left new …
16 Postfix Increment N/A … ++
Postfix Decrement N/A … --
15 Logical not Right-to-left ! …
Bitwise not Right-to-left ~ …
Unary Plus Right-to-left + …
Unary negation Right-to-left - …
Prefix Increment Right-to-left ++ …
Prefix Decrement Right-to-left -- …
typeof Right-to-left typeof …
void Right-to-left void …
Delete Right-to-left delete …
14 exponentiation Right-to-left … ** …
Multiplication Left-to-right … * …
Division Left-to-right … / …
Remainder Left-to-right … % …
13 Addition Left-to-right … + …
Subtraction Left-to-right … - …
12 Bitwise left Shift Left-to-right … << …
Bitwise Right Shift Left-to-right … >> …
Bitwise Unsigned Right Shift Left-to-right … >>> …
11 Less Than Left-to-right … < …
Less Than Or Equal Left-to-right … <= …
Greater Than Left-to-right … > …
Greater Than Or Equal Left-to-right … >= …
Inch Left-to-right … in …
instanceof Left-to-right … instanceof …
10 Equality Left-to-right … == …
Inequality Left-to-right … != …
Strict Equality Left-to-right … === …
Strict inequality Left-to-right … !== …
9 Bitwise and Left-to-right … & …
8 Bitwise XOR Left-to-right … ^ …
7 Bitwise OR Left-to-right … | …
6 Logical and Left-to-right … && …
5 Logical OR Left-to-right … || …
4 Conditional Right-to-left … ? … : …
3 Assignment Right-to-left … = …
… += …
… -= …
… **= …
… *= …
… /= …
… %= …
… <<= …
… >>= …
… >>>= …
… &= …
… ^= …
… |= …
2 Yield Right-to-left yield …
1 Spread N/A ......
0

Php

Operator Precedence
associativity Operators Additional Information
Non-associative Clone new Clone andnew
Left [ Array ()
Right ** Arithmetic
Right + +--~ (int) (float) (string) (array) (object) (BOOL) @ Types Andincrement/decrement
Non-associative instanceof Types
Right ! Logical
Left * / % Arithmetic
Left + - . Arithmetic andstring
Left << >> Bitwise
Non-associative < <= > >= Comparison
Non-associative = = = = = = =!== <> <=> Comparison
Left & Bitwise andreferences
Left ^ Bitwise
Left | Bitwise
Left && Logical
Left || Logical
Right ?? Comparison
Left ? : Ternary
Right = + = = *= **=/=. =%= &= |= ^= <<= >>= Assignment
Left and Logical
Left Xor Logical
Left Or Logical

The above describes the PHP and JavaScript operator precedence differences, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.