( - the function call operator - has higher precedence than ++ and --, but lower precedence than [.
Therefore you can do the following:
<?php
$func[0] = 'exit';
$func[0]();
?>
But the following will cause a syntax error:
<?php
function func() {
return array('string');
}
func()[0];
?>
运算符优先级
运算符优先级指定了两个表达式绑定得有多“紧密”。例如,表达式 1 + 5 * 3 的结果是 16 而不是 18 是因为乘号(“*”)的优先级比加号(“+”)高。必要时可以用括号来强制改变优先级。例如:(1 + 5) * 3 的值为 18。如果运算符优先级相同,则使用从左到右的左联顺序。
下表从高到低列出了运算符的优先级。同一行中的运算符具有相同优先级,此时它们的结合方向决定求值顺序。
| 结合方向 | 运算符 | 附加信息 |
|---|---|---|
| 非结合 | clone new | clone 和 new |
| 左 | [ | array() |
| 非结合 | ++ -- | 递增/递减运算符 |
| 非结合 | ~ - (int) (float) (string) (array) (object) (bool) @ | 类型 |
| 非结合 | instanceof | 类型 |
| 右结合 | ! | 逻辑操作符 |
| 左 | * / % | 算术运算符 |
| 左 | + - . | 算术运算符 和 字符串运算符 |
| 左 | << >> | 位运算符 |
| 非结合 | < <= > >= <> | 比较运算符 |
| 非结合 | == != === !== | 比较运算符 |
| 左 | & | 位运算符 和 引用 |
| 左 | ^ | 位运算符 |
| 左 | | | 位运算符 |
| 左 | && | 逻辑运算符 |
| 左 | || | 逻辑运算符 |
| 左 | ? : | 三元运算符 |
| 右 | = += -= *= /= .= %= &= |= ^= <<= >>= | 赋值运算符 |
| 左 | and | 逻辑运算符 |
| 左 | xor | 逻辑运算符 |
| 左 | or | 逻辑运算符 |
| 左 | , | 多处用到 |
左联表示表达式从左向右求值,右联相反。
Example #1 结合方向
<?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
?>
Note:
尽管 = 比其它大多数的运算符的优先级低,PHP 仍旧允许类似如下的表达式:if (!$a = foo()),在此例中 foo() 的返回值被赋给了 $a。
Christopher Schramm
02-Jul-2011 09:29
charles at pilger dot com dot br
09-Feb-2011 12:00
Be very careful with the precedence. See this code:
<?php
$a = 1;
$b = null;
$c = isset($a) && isset($b);
$d = ( isset($a) and isset($b) );
$e = isset($a) and isset($b);
var_dump($a, $b, $c, $d, $e);
?>
Result:
int(1)
NULL
bool(false)
bool(false)
bool(true) <==
kiamlaluno at avpnet dot org
12-Jul-2010 04:41
Be careful of the difference between
<?php
$obj = new class::$staticVariable();
?>
<?php
$value = class::$staticVariable();
?>
In the first case, the object class will depend on the static variable class::$staticVariable, while in the second case it will be invoked the method whose name is contained in the variable $staticVariable.
headden at karelia dot ru
09-Jun-2009 04:02
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
Pies
08-Feb-2009 11:22
You can use the "or" and "and" keywords' lower precedence for a bit of syntax candy:
<?php
$page = (int) @$_GET['page'] or $page = 1;
?>
