$a = 1;
$b = &$a; //Pass address. Commenting out this line returns 5. If it is not commented out (PHP version <7), it returns 6
$c = ( $a) ( $a);
echo $c;
Does it have anything to do with the version? ? Also, the result is 6, which is really puzzling to me
Let’s first talk about the operation of adding reference assignment
For this operation, it can be considered that both
$a
and$b
point to the memory where the original$a
variable is located (for convenience of explanation later, it is calledmemory X
), that is to say, any subsequent The operations of$a
or$b
are directly modifying the value in this memory.So the running process after adding this line:
The previous steps are omitted, the initial a is 1
Let’s talk about the operation after commenting the reference$c = (++$a) + (++$a)
The two times++$a
are both operatingmemory twice, so when calculating the value of $c, the value of this memory is taken, that is, the value in
memory Xafter it has been incremented twice. Therefore, it is
3 + 3 = 6(when calculating
$c, the value stored in
memory Xis 3).
$a
directly modifiesmemory X
, and then returns a copy of
memory It is not obtained directly fromMemory X
, but a copy after changing the value ofMemory
This is related to the underlying implementation of PHP. It’s a long story. It is recommended to look at the analysis of the same problem on github.
An exploration caused by a PHP bug:
https://github.com/xurenlu/ph...
You output C, what does C have to do with your B?