PHP 與 JavaScript 的運算子優先順序差異

來源:互聯網
上載者:User
兩者優先順序大部分都一樣,比較(comparision)運算和賦值(assignment)運算有細微的差別。比較子有 >, <, >= 等等,賦值運算子有 =, +=, *= 等等。

JS 裡比較子比賦值運算子優先順序高。於是 foo = 1 < 2 結果 foo = false;

PHP 裡反過來,賦值運算子比比較子優先順序高。於是 foo = 1 < 2 結果 foo = 1, 運算式為 false。為達到與上面同樣的結果,需加上小括弧

foo = (1 < 2)

附:PHP與JavaScript完整的運算子優先順序

JS

The following table is ordered from highest (19) 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 … >= …
in 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

以上就介紹了PHP 與 JavaScript 的運算子優先順序差異,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.