PHP 与 JavaScript 的运算符优先级差异

8 篇文章 0 订阅
两者优先级大部分都一样,比较(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.

PrecedenceOperator typeAssociativityIndividual operators
19Groupingn/a( … )
18Member Accessleft-to-right… . …
Computed Member Accessleft-to-right… [ … ]
new (with argument list)n/anew … ( … )
17Function Callleft-to-right… ( … )
new (without argument list)right-to-leftnew …
16Postfix Incrementn/a… ++
Postfix Decrementn/a… --
15Logical NOTright-to-left! …
Bitwise NOTright-to-left~ …
Unary Plusright-to-left+ …
Unary Negationright-to-left- …
Prefix Incrementright-to-left++ …
Prefix Decrementright-to-left-- …
typeofright-to-lefttypeof …
voidright-to-leftvoid …
deleteright-to-leftdelete …
14Exponentiationright-to-left… ** …
Multiplicationleft-to-right… * …
Divisionleft-to-right… / …
Remainderleft-to-right… % …
13Additionleft-to-right… + …
Subtractionleft-to-right… - …
12Bitwise Left Shiftleft-to-right… << …
Bitwise Right Shiftleft-to-right… >> …
Bitwise Unsigned Right Shiftleft-to-right… >>> …
11Less Thanleft-to-right… < …
Less Than Or Equalleft-to-right… <= …
Greater Thanleft-to-right… > …
Greater Than Or Equalleft-to-right… >= …
inleft-to-right… in …
instanceofleft-to-right… instanceof …
10Equalityleft-to-right… == …
Inequalityleft-to-right… != …
Strict Equalityleft-to-right… === …
Strict Inequalityleft-to-right… !== …
9Bitwise ANDleft-to-right… & …
8Bitwise XORleft-to-right… ^ …
7Bitwise ORleft-to-right… | …
6Logical ANDleft-to-right… && …
5Logical ORleft-to-right… || …
4Conditionalright-to-left… ? … : …
3Assignmentright-to-left… = …
… += …
… -= …
… **= …
… *= …
… /= …
… %= …
… <<= …
… >>= …
… >>>= …
… &= …
… ^= …
… |= …
2yieldright-to-leftyield …
1Spreadn/a... …
0

PHP
Operator Precedence
AssociativityOperatorsAdditional Information
non-associativeclone newclone and new
left[array()
right**arithmetic
right++ -- ~ (int) (float) (string) (array) (object) (bool) @types and increment/decrement
non-associativeinstanceoftypes
right!logical
left* / %arithmetic
left+ - .arithmetic and string
left<< >>bitwise
non-associative< <= > >=comparison
non-associative== != === !== <> <=>comparison
left&bitwise and references
left^bitwise
left|bitwise
left&&logical
left||logical
right??comparison
left? :ternary
right= += -= *= **= /= .= %= &= |= ^= <<= >>= =>assignment
leftandlogical
leftxorlogical
leftorlogical
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值