|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-08-21 16:54 UTC] colder@php.net
[2009-08-21 17:00 UTC] anshul at designgrill dot com
[2009-08-21 17:08 UTC] colder@php.net
[2009-08-21 17:21 UTC] anshul at designgrill dot com
[2009-08-21 21:09 UTC] colder@php.net
[2009-08-21 21:27 UTC] anshul at designgrill dot com
[2009-08-21 22:56 UTC] anshul at designgrill dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 23:00:01 2025 UTC |
Description: ------------ If you keep reference to an element of the array before the copy operation using = operator, you can still modify both the arrays using the reference copied earlier. The same doesn't happen if you keep the reference after the copy operation and try to modify the variable using it. Reproduce code: --------------- <?php $arr1 = array(1, 2); $var = &$arr1[0];//Keeping the reference $arr2 = $arr1;//Copy operation $var = 10;//Will modify both the arrays, bad print_r($arr1); print_r($arr2); ?> Expected result: ---------------- Array ( [0] => 10 [1] => 2 ) Array ( [0] => 1 [1] => 2 ) Actual result: -------------- Array ( [0] => 10 [1] => 2 ) Array ( [0] => 10 [1] => 2 )