我不明白为什么这条指令会输出5.以及PHP解释器将如何执行它。
$a = (1 << 0);
$b = (1 << $a);
$c = (1 << $b);
echo ($c || $b) << 2 * $a | $a; //5
答案 0 :(得分:1)
它的解决方式如下:
echo (($c || $b) << (2 * $a)) | $a;
这只是我测试的一些输出:
php > $a = (1 << 0);
php > echo $a;
1
php > $b = (1 << $a);
php > echo $b;
2
php > $c = (1 << $b);
php > echo $c;
4
php > echo ($c || $b);
1
php > echo ($c || $b) << 2 * $a | $a;
5
php > echo (($c || $b) << (2 * $a)) | $a;
5
答案 1 :(得分:1)
$a = (1 << 0); // 1
$b = (1 << $a); // doesnt matter, not used
$c = (1 << $b); // doesnt matter, not used
echo ($c || $b) << 2 * $a | $a; //5
// ($c || $b) will return true, meaning 1
// so you can rewrite it as
echo 1 << 2 * $a | $a
// $a = 1, so:
echo 1 << 2 * 1 | 1
// or:
echo 1 << 2 | 1
// ->
echo 4 | 1 // = 5