Associativity | Operators | Additional Information |
---|---|---|
non-associative | clone new | clone and new |
left | [ | array() |
non-associative | ++ -- | increment/decrement |
non-associative | ~ - (int) (float) (string) (array) (object) (bool) @ | types |
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 |
left | ? : | ternary |
right | = += -= *= /= .= %= &= |= ^= <<= >>= | assignment |
left | and | logical |
left | xor | logical |
left | or | logical |
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: 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.
如果真的不想背这个表的话。 直接在“()”就行了。省的出现错误。