为什么if ($a = 100 && $b = 200) {var_dump($a, $b);}
输出bool(true) int(200)
<?php
if ($a = 100 && $b = 200) {
var_dump($a, $b);
}
为什么if ($a = 100 && $b = 200) {var_dump($a, $b);}
输出bool(true) int(200)
<?php
if ($a = 100 && $b = 200) {
var_dump($a, $b);
}
1 回答4.1k 阅读✓ 已解决
3 回答1.8k 阅读✓ 已解决
2 回答2.2k 阅读✓ 已解决
1 回答1.4k 阅读✓ 已解决
2 回答2.2k 阅读
1 回答592 阅读✓ 已解决
792 阅读
&&
优先级比=
高,所以会$a = (100 && $b = 200)
,在http://php.net/manual/en/language.operators.precedence.php中有一句Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.
,虽然&&
优先级高于=
,但是200
还是会被赋值给$b
,由于100 && $b = 200
均为真返回true
,所以$a=true