An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value (so that the construction itself becomes an expression). So you can think of functions or constructions that return a value (like print) as operators and those that return nothing (like echo) as any other thing.
There are three types of operators. Firstly there is the unary operator which operates on only one value, for example ! (the negation operator) or ++ (the increment operator). The second group are termed binary operators; this group contains most of the operators that PHP supports, and a list follows below in the section Operator Precedence.
The third group is the ternary operator: ?:. It should be used to select between two expressions depending on a third one, rather than to select two sentences or paths of execution. Surrounding ternary expressions with parentheses is a very good idea.
演算子の優先順位は、二つの式が"緊密に"結合している度合いを指定します。 例えば、式 1 + 5 * 3 の答えは 16になり、18とはなりません。 これは乗算演算子("*")は、加算演算子("+")より高い優先順位を有するか らです。必要に応じて強制的に優先順位を設定するために括弧を使用する ことが可能です。例えば、18と評価するためには、 (1 + 5) * 3 とします。 If operator precedence is equal, left to right associativity is used.
The following table lists the precedence of operators with the highest-precedence operators listed at the top of the table. Operators on the same line have equal precedence, in which case their associativity decides which order to evaluate them in.
表 15-1. 演算子の優先順位
結合時の評価 | 演算子 |
---|---|
結合しない | new |
right | [ |
結合しない | ++ -- |
結合しない | ! ~ - (int) (float) (string) (array) (object) @ |
left | * / % |
left | + - . |
left | << >> |
結合しない | < <= > >= |
結合しない | == != === !== |
left | & |
left | ^ |
left | | |
left | && |
left | || |
left | ? : |
right | = += -= *= /= .= %= &= |= ^= <<= >>= |
left | and |
left | xor |
left | or |
left | , |
Left associativity means that the expression is evaluated from left to right, right associativity means the opposite.
注意: !は=よりも優先されるはずなの にもかかわらず、PHPは依然としてif (!$a = foo()) のような式も許します。この場合はfoo()の出力が $aに代入されます。