演算子の優先順位
演算子の優先順位は、二つの式が"緊密に"結合している度合いを指定します。
例えば、式 1 + 5 * 3
の答えは
16
になり、18
とはなりません。
これは乗算演算子("*")は、加算演算子("+")より高い優先順位を有するか
らです。必要に応じて強制的に優先順位を設定するために括弧を使用する
ことが可能です。例えば、18
と評価するためには、
(1 + 5) * 3
とします。
演算子の優先順位が等しい場合は、その結合性によって評価順
(右から評価するのか、あるいは左から評価するのか) が決まります。
たとえば "-" は左結合なので、
1 - 2 - 3
は (1 - 2) - 3
とグループ分けされて、
評価結果は -4
になります。
一方、"=" は右結合です。つまり $a = $b = $c
のグループ分けは
$a = ($b = $c)
となります。
優先順位が同じで結合しない演算子を並べることはできません。つまり、たとえば
1 < 2 > 1
は PHP では無効になります。
一方 1 <= 1 == 1
は問題ありません。
==
演算子の優先順位が
<=
演算子より低いからです。
演算子の結合は、
二項演算子(および三項演算子) の場合にだけ意味があります。
単項演算子は、前置または後置しかないため、
演算子の結合の考え方は適用できません。
たとえば、
!!$a
は、
!(!$a)
という形でのみグループ化できます。
厳密には不要な場所であっても、括弧をつけておけばコードの可読性があがります。 明示的にグループ分けをしておくことで、演算子の優先順位や結合性による暗黙のグループ分けに頼らずに済むからです。
次の表では、優先順位が高い順に演算子を挙げています。 同じ行にある演算子は優先順位が等しくなります。そのような場合は、 結合時の評価にしたがってグループ分けが決まります。
結合時の評価 | 演算子 | 追加情報 |
---|---|---|
(n/a) |
clone
new
|
clone および new |
right | ** |
算術演算子 |
(n/a) |
+
-
++
--
~
(int)
(float)
(string)
(array)
(object)
(bool)
@
|
算術演算子 (単項演算の + と - ),
加算子/減算子,
ビット演算子,
型の相互変換 そして
エラー制御演算子
|
left | instanceof |
型演算子 |
(n/a) | ! |
論理演算子 |
left |
*
/
%
|
算術演算子 |
left |
+
-
.
|
算術演算子 (二項演算の + と - ),
配列演算子 そして
文字列演算子 (PHP 8.0.0 より前のバージョンの. )
|
left |
<<
>>
|
ビット演算子 |
left | . |
文字列演算子 (PHP 8.0.0 以降) |
結合しない |
<
<=
>
>=
|
比較演算子 |
結合しない |
==
!=
===
!==
<>
<=>
|
比較演算子 |
left | & |
ビット演算子 そして リファレンス |
left | ^ |
ビット演算子 |
left | | |
ビット演算子 |
left | && |
論理演算子 |
left | || |
論理演算子 |
right | ?? |
NULL合体演算子 |
結合しない | ? : |
三項演算子 (PHP 8.0.0 より前のバージョンでは、左結合でした) |
right |
=
+=
-=
*=
**=
/=
.=
%=
&=
|=
^=
<<=
>>=
??=
|
代入演算子 |
(n/a) | yield from |
yield from |
(n/a) | yield |
yield |
(n/a) | print |
|
left | and |
論理演算子 |
left | xor |
論理演算子 |
left | or |
論理演算子 |
例1 結合時の評価
<?php
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
// PHP の三項演算子の結合法則は、C/C++のそれとは異なります
$a = true ? 0 : true ? 1 : 2; // PHP 8.0.0 より前のバージョンでは、(true ? 0 : true) ? 1 : 2 = 2
$a = 1;
$b = 2;
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
?>
演算子の優先順位や結合性は、あくまでも式のグループ分けだけを決めるものであり、評価順を決めるものではありません。 PHP では一般に、式をどの順番で評価するかは決めていません。 そのため、特定の順序で式が評価されることを前提としたコードを書いてはいけません。 PHP のバージョンが変わったり前後のコードが変わったりしたときに、評価順が変わる可能性があるからです。
例2 評価順序は未定義
<?php
$a = 1;
echo $a + $a++; // 2 になるかもしれないし、3 になるかもしれません
$i = 1;
$array[$i] = $i++; // インデックス 1 をセットするかもしれないし、インデックス 2 をセットするかもしれません
?>
例3 +
、-
、.
の優先順位は同じ(PHP 8.0.0 より前のバージョン)
<?php
$x = 4;
// 次の行は、予期せぬ結果になることでしょう
echo "x minus one equals " . $x-1 . ", or so I hope\n";
// なぜなら、これは次のように評価されるからです(PHP 8.0.0より前のバージョン)
echo (("x minus one equals " . $x) - 1) . ", or so I hope\n";
// 期待どおりの結果を得るには、括弧を使って優先順位を指定します
echo "x minus one equals " . ($x-1) . ", or so I hope\n";
?>
上の例の出力は以下となります。
-1, or so I hope -1, or so I hope x minus one equals 3, or so I hope
注意:
=
は他のほとんどの演算子よりも優先順位が低いはずなのにもかかわらず、 PHP は依然としてif (!$a = foo())
のような式も許します。この場合はfoo()
の戻り値が $a に代入されます。
変更履歴
バージョン | 説明 |
---|---|
8.0.0 |
文字列の連結 (. )
の優先順位は、
算術演算子の加算/減算
(+ および - )
と ビットシフト演算子
(<< および >> );
よりも低くなりました。
これより前のバージョンでは、文字列の連結は
+ と -
と同じ優先順位を持ち、
<< と >>
よりは高い優先順位を持っていました。
|
8.0.0 |
三項演算子 (? : ) は、
どの演算とも結合しなくなりました。
これより前のバージョンでは、左結合でした。
|
7.4.0 |
三項演算子(? : ) が左結合であることに依存すること、
つまり、括弧で囲わずに三項演算子をネストすることは推奨されなくなりました。
|
7.4.0 |
文字列の連結 (. ) 演算と、
算術演算子の加算/減算
(+ および - )
と ビットシフト演算子
(<< および >> );
の優先順位に依存するコードは、推奨されなくなりました。
つまり、これらを括弧を使わずに一緒に使うことは推奨されなくなりました。
|
User Contributed Notes 9 notes
Watch out for the difference of priority between 'and vs &&' or '|| vs or':
<?php
$bool = true && false;
var_dump($bool); // false, that's expected
$bool = true and false;
var_dump($bool); // true, ouch!
?>
Because 'and/or' have lower priority than '=' but '||/&&' have higher.
Note that ?? has a low priority, so this can lead to unexpected results:
$a=[];
$a['aa']??'not set'
--> not set (as expected)
but
"lets see if it is set".$a['aa']??'not set'
--> notice; undefined index aa
--> lets see if it is set
so you need to use parenthesis
"lets see if it is set".($a['aa']??'not set')
If you've come here looking for a full list of PHP operators, take note that the table here is *not* complete. There are some additional operators (or operator-ish punctuation tokens) that are not included here, such as "->", "::", and "...".
For a really comprehensive list, take a look at the "List of Parser Tokens" page: http://php.net/manual/en/tokens.php
Beware the unusual order of bit-wise operators and comparison operators, this has often lead to bugs in my experience. For instance:
<?php if ( $flags & MASK == 1) do_something(); ?>
will not do what you might expect from other languages. Use
<?php if (($flags & MASK) == 1) do_something(); ?>
in PHP instead.
Using cast and ternary operator can be unclear,
(Useful to know with: declare(strict_types = 1) ).
<?php
$num_str="5";
$i1 = (int) isset($num_str) ? $num_str : 0;
$i2 = (int) (isset($num_str) ? $num_str : 0);
var_dump($i1);
var_dump($i2);
?>
Output:
string(1) "5"
int(5)
<?php
// Another tricky thing here is using && or || with ternary ?:
$x && $y ? $a : $b; // ($x && $y) ? $a : $b;
// while:
$x and $y ? $a : $b; // $x and ($y ? $a : $b);
?>
An easy trick to get the result of the left shift operation (<<), e.g.
15 << 2 = 15 * (2*2) = 60
15 << 3 = 15 * (2*2*2) = 120
15 << 5 = 15 * (2*2*2*2*2) = 480
and so on...
So it's:
(number on left) multiplied by (number on right) times 2.
The same goes for the right shift operator (>>), where:
(number on left) divided by (number on right) times 2 e.g.
15 >> 2 = (15/2)/2 = 7/2 = 3 (use floor values if result is in decimals).
35 >> 3 = (((35/2)/2)/2 = (17/2)/2 = 8/2 = 4
//incorrect
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2
//Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`
//correct
$a = (true ? 0 : true) ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2
==> correction documentation.
The main use of "or" and "and" is that they have a lower precedence than "=", which is useful for error-handling, with short-circuiting. So, for example:
$conn = pg_connect( ... ) or error_log ("Error, could not connect");
* If pg_connect() is successful, $conn contains the connection-object, and no error is logged.
* If pg_connect() fails, an error is logged, but $conn is FALSE, as we would wish, even though the error_log() function is called, and returns true.