ID:               49322
 User updated by:  anshul at designgrill dot com
 Reported By:      anshul at designgrill dot com
 Status:           Bogus
 Bug Type:         Arrays related
 Operating System: All
 PHP Version:      5.2.10
 New Comment:

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);
?>


Previous Comments:
------------------------------------------------------------------------

[2009-08-21 16:54:32] col...@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 16:52:29] 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
)



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=49322&edit=1

Reply via email to