Copy On Write and Assign By Reference perform different on PHP5 and PHP7
| From: | Huqiu Liao | Date: | Thu, 31 Mar 2016 16:47:28 +0000 |
| Subject: | Copy On Write and Assign By Reference perform different on PHP5 and PHP7 | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-92050@lists.php.net to get a copy of this message | ||
Hi, internals,
I have a question about Assign By Reference and I posted on StackOverflow,
I'd like to know the reason behind it, and I did not get any this kind of
answer, can anyone give me some clues.
---
We have a piece of simple code:
1 <?php
2 $i = 2;
3 $j = &$i;
4 echo (++$i) + (++$i);
On PHP5, it outputs 8, because:
$i is a reference, when we increase $i by ++i, it will change the zval
rather than make a copy, so line 4 will be 4 + 4 = 8. This is Assign By
Reference.
If we comment line 3, it will output 7, every time we change the value by
increasing it, PHP will make a copy, line 4 will be 3 + 4 = 7. This is Copy
On Write.
But in PHP7, it always outputs 7.
I've checked the changes in PHP7:
http://php.net/manual/en/migration70.incompatible.php,
but I did not get
any clue.
Here is the result of the code on PHP5 / PHP7: https://3v4l.org/USTHR
And there is the link on Stackoverflow:
http://stackoverflow.com/questions/36338661/php-copy-on-write-and-assign-by-reference-perform-different-on-php5-and-php7
Thanks guys.
Best,
--
Huqiu Liao
http://www.liaohuqiu.net