运算符优先级

** 算术运算符

instanceof 类型

* / % 算术运算符
+ - . 算数 (二元 +-)， arraystring. PHP 8.0.0 前可用）
<< >> 位运算符
. string （PHP 8.0.0 起可用）
< <= > >= 比较运算符
== != === !== <> <=> 比较运算符
& 位运算符引用
^ 位运算符
| 位运算符
&& 逻辑运算符
|| 逻辑运算符
?? null 合并运算符

= += -= *= **= /= .= %= &= |= ^= <<= >>= ??= 赋值运算符

and 逻辑运算符
xor 逻辑运算符
or 逻辑运算符

<?php
\$a
5// (3 * 3) % 5 = 4
// PHP 的三元操作符跟 C/C++ 有区别
\$a true true 2// (true ? 0 : true) ? 1 : 2 = 2 (PHP 8.0.0 前可用)

\$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-", 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

更新日志

8.0.0 现在，字符串连接符（.）的优先级比算数加/减（+-）、按位左/右移（<<>>）更低。在此之前，它的优先级与 +- 相同，并且比 <<>> 更高。
8.0.0 三元运算符（? :）是现在是非关联的；以前它是左联的。
7.4.0 已弃用在无括号的表达式中依赖字符串连接（.）相对于算数加/减（+ 或者 -）或者按位左/右移（<< 或者 >>）的优先级的使用方法。
7.4.0 不推荐使用三元运算符（? :）的左联。 即已弃用嵌套多个未带括号的三元运算符。
170
fabmlk
6 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.
27
aaronw at catalyst dot net dot nz
4 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
48
Carsten Milkau
9 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(); ?>

11
ivan at dilber dot info
4 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);

?>
instatiendaweb at gmail dot com
11 months ago
//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.
noone
1 year 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 5

You'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  5

We 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.
karlisd at gmail dot com
6 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:

<?php
function 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
anisgazig at gmail dot com
11 months ago
Three types of operator associativity in php.
1.left
2.rigt
3.non-associativity

Category of three operators are right associativity
1)**
2)=,+=,-=,*=,/=,%=,&=,^=,|=,<<=,>>=,??=,.=
3)??

Category of eight operators are non-associativity
1)clone new
2)++,--,~,@
3)!
4)<,<=,>,>=
5)<<,>>
6)yield from
7)yield
8)print

Rest of the operators are left associativity 