21,893
社区成员




echo (++$a) + (++$a) + (++$a)."\n";//这里输出的是10,而不是2+3+4=9
<?php
$a = 1;
$b = &$a;
echo (++$a) + (++$a) + (++$a)."\n";
?>
php7.0.7
Finding entry points
Branch analysis from position: 0
Return found
filename: E:\xampp\htdocs\test\opcode.php
function name: (null)
number of ops: 9
compiled vars: !0 = $a, !1 = $b
line # * op fetch ext return operands
---------------------------------------------------------------------------------
3 0 > ASSIGN !0, 1
4 1 ASSIGN !1, !0
5 2 PRE_INC $0 !0
3 PRE_INC $2 !0
4 ADD ~1 $0, $2
5 PRE_INC $2 !0
6 ADD ~0 ~1, $2
7 ECHO ~0
8 > RETURN 1
branch: # 0; line: 3- 5; sop: 0; eop: 8
path #1: 0,
9
加入引用之后是这样:
Finding entry points
Branch analysis from position: 0
Return found
filename: E:\xampp\htdocs\test\opcode.php
function name: (null)
number of ops: 9
compiled vars: !0 = $a, !1 = $b
line # * op fetch ext return operands
---------------------------------------------------------------------------------
3 0 > ASSIGN !0, 1
4 1 ASSIGN_REF !1, !0
5 2 PRE_INC $0 !0
3 PRE_INC $2 !0
4 ADD ~1 $0, $2
5 PRE_INC $2 !0
6 ADD ~0 ~1, $2
7 ECHO ~0
8 > RETURN 1
branch: # 0; line: 3- 5; sop: 0; eop: 8
path #1: 0,
10
两者唯一的差别是使用的宏不同,这时你就会发现当执行到第四步的时候$0是$a的引用,但你在第三步的时候就已经更改了$0,所以此时就变成了3+3所以结果就是6而不是5,PHP7没有环境所以还不清楚,你可以自行试一下。
$a = 1;
$b =& $a;
echo (++$a) + (++$a) + (++$a); //10