php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49322 Incorrect array assignment
Submitted: 2009-08-21 16:52 UTC Modified: 2009-08-21 22:56 UTC
From: anshul at designgrill dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.10 OS: All
Private report: No CVE-ID: None
 [2009-08-21 16:52 UTC] anshul at designgrill dot com
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
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-21 16:54 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

That's expected, arrays have always worked like that.
 [2009-08-21 17:00 UTC] anshul at designgrill dot com
Following will give a different result. Just because reference is copying after assignment.

PHP manual says the assignment operation copies by value for arrays.

<?php
$arr1 = array(1, 2);
$arr2 = $arr1;
$var = &$arr1[0];

$var = 10;
print_r($arr1);
print_r($arr2);
?>
 [2009-08-21 17:08 UTC] colder@php.net
Yes, the array itself is copied by value, but not the array content, as you discovered.

$a = array();
$b = $a; 
$b[] = 2; var_dump(count($a)); // int(0)
 [2009-08-21 17:21 UTC] anshul at designgrill dot com
The example you gave behaves expectedly and actually denotes the bug is valid. I would request you to carefully go through the two code snippets I gave.

Even when you copy a value by reference the array, the copy is not actually made and instead the refcount is increased of the array. PHP will defer the actual copy until any one of the array is actually modified. 

Now when you copy the reference to the any array item, and modify using that reference, a new copy is created and values updated. But if you keep the reference of the item in a variable before array copying, modifying this variable will not force PHP to create the actual copy of the array.

Incorrect behavior at the best.
 [2009-08-21 21:09 UTC] colder@php.net
Again, those are expected results, when you copy an array, its  elements that are references will be copied as references. It has always been like that.
 [2009-08-21 21:27 UTC] anshul at designgrill dot com
Ok.
But it is not obvious from the documentation. A cautionary statement should be included in there.
 [2009-08-21 22:56 UTC] anshul at designgrill dot com
Moreover, variables, which are references, when copied are not copied as reference and instead a true copy is made. Array elements should not be treated differently.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC