运算符优先级
运算符优先级指定了两个表达式绑定得有多“紧密”。例如,表达式 1 + 5 *
3 的结果是 16 而不是 18
是因为乘号(“*”)的优先级比加号(“+”)高。必要时可以用括号来强制改变优先级。例如:(1
+ 5) * 3 的值为 18。
如果运算符优先级相同,那运算符的结合方向决定了该如何运算。例如,"-"是左联的,那么 1 - 2 - 3 就等同于 (1 - 2) - 3
并且结果是 -4. 另外一方面,"="是右联的,所以 $a = $b = $c 等同于
$a = ($b = $c)。
没有结合的相同优先级的运算符不能连在一起使用,例如
1 < 2 > 1 在PHP是不合法的。但另外一方面表达式
1 <= 1 == 1 是合法的, 因为 == 的优先级低于
<=。
关联性仅对二(三)元操作符有意义。
一元操作符是前缀或者后缀,所以不适用该概念。
例如 !!$a 仅可分为 !(!$a)。
括号的使用,哪怕在不是必要的场合下,通过括号的配对来明确标明运算顺序,而非靠运算符优先级和结合性来决定,通常能够增加代码的可读性。
下表按照优先级从高到低列出了运算符。同一行中的运算符具有相同优先级,此时它们的结合方向决定求值顺序。
示例 #1 结合方向
<?php
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
var_dump($a);
$a = 1;
$b = 2;
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
var_dump($a, $b);
?>
三元运算符特别要求使用括号来消除优先级歧义。
示例 #2 明确优先级
<?php
$a = true ? 0 : (true ? 1 : 2);
var_dump($a);
// 自 PHP 8 起,这样不允许
// $a = true ? 0 : true ? 1 : 2;
?>
运算符优先级和关联方式仅决定表达式如何分组,不指定计算顺序。
一般情况下, PHP 不指定表达式的计算顺序,并且代码避免指定假设执行顺序,
因为行为会在 PHP 版本间发生变化或者依赖于旁边的代码。
示例 #3 未定义执行顺序
<?php
$a = 1;
echo $a + $a++; // 可能会输出 2 或 3
$i = 1;
$array[$i] = $i++; // 可能会设置索引 1 或 2
?>
示例 #4 +、- 和 . 优先级
<?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";
// 这样不允许,会抛出 TypeError:
echo (("x minus one equals " . $x) - 1) . ", or so I hope\n";
?>
-1, or so I hope
-1, or so I hope
Fatal error: Uncaught TypeError: Unsupported operand types: string - int
示例 #5 PHP 8 之前,+、- 和 . 具有相同的优先级
<?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。
fabmlk ¶11 years ago
Watch out for the difference of priority between 'and vs &&' or '|| vs or':
<?php
$bool = true && false;
var_dump($bool); $bool = true and false;
var_dump($bool); ?>
Because 'and/or' have lower priority than '=' but '||/&&' have higher.
rvwoens at gmail dot com ¶3 years ago
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')
aaronw at catalyst dot net dot nz ¶8 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
Carsten Milkau ¶13 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.
tlili dot mokhtar at gmail dot com ¶4 years ago
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
sangala at seznam dot cz ¶3 years ago
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)
instatiendaweb at gmail dot com ¶5 years 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.