Bulgaria PHP Conference 2019

## 演算子の優先順位

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

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

left & ビット演算子 そして リファレンス
left ^ ビット演算子
left | ビット演算子
left && 論理演算子
left || 論理演算子
right ?? 比較演算子
left ? : 三項演算子
right = += -= *= **= /= .= %= &= |= ^= <<= >>= 代入演算子
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 10 notes

130
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. ```
18
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 ```
41
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. ```
wbrzezin
1 year ago
``` null coalescing `??` is between logic or `||`    and ternary `? :` ```
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
3 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. ```
-2
headden at karelia dot ru
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 ```
-3
kitchin
2 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.?> ```
-16
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! ```
-13
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()?> ``` 