PHP与JavaScript的运算符优先级差异
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP与JavaScript的运算符优先级差异,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2792字,纯文字阅读大概需要4分钟。
内容图文
两者优先级大部分都一样,比较(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
Associativity | Operators | Additional Information |
---|---|---|
non-associative | clone new | clone and new |
left | [ | array() |
right | ** | arithmetic |
right | ++ -- ~ (int) (float) (string) (array) (object) (bool) @ | types and increment/decrement |
non-associative | instanceof | types |
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 |
left | and | logical |
left | xor | logical |
left | or | logical |
以上就介绍了PHP 与 JavaScript 的运算符优先级差异,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
内容总结
以上是互联网集市为您收集整理的PHP与JavaScript的运算符优先级差异全部内容,希望文章能够帮你解决PHP与JavaScript的运算符优先级差异所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。