## 演算子の優先順位

right ** 代数演算子
right ++ -- ~ (int) (float) (string) (array) (object) (bool) @ および 加算子/減算子

right ! 論理演算子
left * / % 代数演算子
left + - . 代数演算子 そして 文字列演算子
left << >> ビット演算子

left & ビット演算子 そして リファレンス
left ^ ビット演算子
left | ビット演算子
left && 論理演算子
left || 論理演算子
right ?? NULL合体演算子
left ? : 三項演算子
right = += -= *= **= /= .= %= &= |= ^= <<= >>= ??= 代入演算子
right yield from yield from
right yield yield
left and 論理演算子
left xor 論理演算子
left or 論理演算子

``` <?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?> ```

``` <?php\$a = 1;echo \$a + \$a++; // 2 になるかもしれないし、3 になるかもしれません\$i = 1;\$array[\$i] = \$i++; // インデックス 1 をセットするかもしれないし、インデックス 2 をセットするかもしれません?> ```

``` <?php\$x = 4;// 次の行は、予期せぬ結果になることでしょうecho "x minus one equals " . \$x-1 . ", or so I hope\n";// なぜなら、これは次のように評価されるからです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 に代入されます。 add a note

### User Contributed Notes 11 notes

149
fabmlk
4 years ago
``` 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. ```
21
aaronw at catalyst dot net dot nz
2 years ago
``` 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 ```
42
Carsten Milkau
7 years ago
``` 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. ```
ivan at dilber dot info
2 years ago
``` <?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);?> ```
karlisd at gmail dot com
4 years ago
``` Sometimes it's easier to understand things in your own examples.If you want to play around operator precedence and look which tests will be made, you can play around with this:<?phpfunction F(\$v) {echo \$v." "; return false;}function T(\$v) {echo \$v." "; return true;}IF (F(0) || T(1) && F(2)  || F(3)  && ! F(4) ) {  echo "true";} else echo " false";?>Now put in IF arguments f for false and t for true, put in them some ID's. Play out by changing "F" to "T" and vice versa, by keeping your ID the same. See output and you will know which arguments  actualy were checked. ```
noone
1 month ago
``` Something that threw me of guard and I hadn't found it mentioned anywhere is if you're looking to asign a value in an if statement condition and use the same value in the said condition and compare it to a different value note the precedence of operators.if(\$a=5&&\$a==5){  echo '5';} else {  echo 'not 5';}//echos  not 5You'll get a Notice:  Undefined variable: a; This happens because the expression is treated as (\$a=5&&(\$a==5))In this case \$a was undefined.Use parentheses to enforce the desired outcome or and instead of &&.if((\$a=5)&&\$a==5){ // or \$a=5 and \$a==5  echo '5';} else {  echo 'not 5';}//echos  5We get no notice!A use case for this can be a three part condition that first checks if a value is valid, second assigns a new variable based on the first value and then checks if the result is valid.\$ID=100;if (\$ID&&(\$data=get_table_row_for_ID(\$ID))&&\$data->is_valid()) { //NOTE: assigned \$data // do something with the data}If assigning variables in an if condition I recommend adding a comment at the end of the line that such an action took place. ```
-2
wbrzezin
2 years ago
``` null coalescing `??` is between logic or `||`    and ternary `? :` ```
-6
10 years ago
``` Although example above already shows it, I'd like to explicitly state that ?: associativity DIFFERS from that of C++. I.e. convenient switch/case-like expressions of the form\$i==1 ? "one" :\$i==2 ? "two" :\$i==3 ? "three" :"error";will not work in PHP as expected ```
-8
kitchin
3 years ago
``` The precedence of '->' is less than '[' in this situation: object contains array, name of array is stored in string variable.<?php\$farm = new StdClass;\$farm->emu = array( 'name' => 'Henry', 'age' => 9 );\$farm->rabbit = array( 'name' => 'George', 'age' => 4 );\$animal = 'rabbit';print_r( \$farm->\$animal ); // ok// print( \$farm->\$animal[ 'name' ] );  // wrong, [ has precedence.print( \$farm->{\$animal}[ 'name' ] ); // correct, prints George.\$farm->wash = 'Suds';\$jobs = array( 'morning' => 'feed', 'evening' => 'wash' );print( \$farm->\$jobs[ 'evening' ] ); // correct, prints Suds.print( \$farm->{\$jobs[ 'evening' ]} ); // correct, prints Suds.?> ```
-17
leipie at gmail dot com
6 years ago
``` The precedence of the arrow operator (->) on objects seems to the highest of all, even higher then clone. But you can't wrap (clone \$foo)->bar() like this! ```
-18
ohcc at 163 dot com
2 years ago
``` Syntax (new Person())->talk(); is supported as of PHP 5.5<?php    class A {        public \$b = 'B';        public function b(){            return 'Bee';        }    }    \$a = new A;    new \$a->b();// This means new B() rather than new Bee()?> ``` 