This article uses the VLD tool to analyze the php opcode to explain the reason, first Map & amp; lt ;? Php $ i1; $ I + $ I ++; numberofops: 5 compiledvars :! 0 $ iline # * opfetchextreturnoperands -------... this article uses the VLD tool to analyze the php opcode, to explain the cause, first map
T1.php code
$ I = 1;
$ I + $ I ++;
Vld code
Number of ops: 5
Compiled vars :! 0 = $ I
Line # * op fetch ext return operands
---------------------------------------------------------------------------------
2 0> ASSIGN! 0, 1
3 1 POST_INC ~ 1! 0
2 ADD ~ 2! 0 ,~ 1
3 FREE ~ 2
4 4> RETURN 1
Branch: #0; line: 2-4; sop: 0; eop: 4
Path #1: 0,
T2.php code
$ I = 1;
$ I + $ I ++
Vld2 code
Number of ops: 6
Compiled vars :! 0 = $ I
Line # * op fetch ext return operands
---------------------------------------------------------------------------------
2 0> ASSIGN! 0, 1
3 1 ADD ~ 1! 0 ,! 0
2 POST_INC ~ 2! 0
3 ADD ~ 3 ~ 1 ,~ 2
4 FREE ~ 3
4 5> RETURN 1
Branch: #0; line: 2-4; sop: 0; eop: 5
Path #1: 0,
We can compare the two pictures of 2, 4 to see why the results surprised you.
First, analyze the execution results of 1 and 2.
First, assign $ I to 1, $ I ++ auto-increment, and copy result 1 to the temporary variable ~ 1 ($ I), then $ I is automatically increased to 2, that is! 0 = 2. of course, the final result ~ 2 = ~ 1 +! 0 = 3;
Analyze 3, 4
First, assign $ I to 1, and then $ I + $ I =! 0 +! 0 = ~ 1 = 2, and then $ I ++ auto-increment, copy result 1 to the temporary variable ~ 2 ($ I), the final result ~ 1 + ~ 2 = 2 + 1 = 3;