您的位置:首页 > 编程语言 > PHP开发

PHP 运算符 优先级

2010-07-28 11:00 369 查看
Operator Precedence
AssociativityOperatorsAdditional Information
non-associativeclone newclone and new
left[array()
non-associative++ --increment/decrement
non-associative~ - (int) (float) (string) (array) (object) (bool) @types
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
left? :ternary
right= += -= *= /= .= %= &= |= ^= <<= >>= assignment
leftandlogical
leftxorlogical
leftorlogical
left,many uses
Left associativity means that the expression is evaluated from left to right, right associativity means the opposite.

Example #1 Associativity

<?php
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2

$a = 1;
$b = 2;
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
?>


Use parentheses to increase readability of the code.


Note[/b]: Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.



如果真的不想背这个表的话。 直接在“()”就行了。省的出现错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: