[PHP-DOC] #20993 [Sus->Opn]: Element value changes without asking From: msopacua <email protected>
Date: 12/18/02

 ID: 20993
 Updated by: msopacua <email protected>
 Reported By: henrik.gebauer <email protected>
-Status: Suspended
+Status: Open
-Bug Type: Scripting Engine problem
+Bug Type: Documentation problem
 Operating System: Any
 PHP Version: 4.0CVS-2002-12-13
 New Comment:

We have discussed this issue and it will put a considerable slowdown on
php's performance, to fix this properly.

Therefore this behavior will be documented.

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

[2002-12-13 14:51:47] moriyoshi <email protected>

This bug has a lot to do with the bug reports mentioned below.

http://bugs.php.net/bug.php?id=6417
http://bugs.php.net/bug.php?id=7412
http://bugs.php.net/bug.php?id=15025

As a workaround, you can write as following to avoid this issue.

function theFunction($array) {
    $array = unserialize(serialize($array));
    
    $array[0] = 2;
}

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

[2002-12-13 12:50:26] moriyoshi <email protected>

Verified with 4.2.3

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

[2002-12-13 12:42:22] msopacua <email protected>

Verified and added testcase to CVS

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

[2002-12-13 12:01:33] henrik.gebauer <email protected>

I create an array an then a reference to an element of that array.
Then the array is passed to a function (by value!) which changes the
value of the element.
After that, the global array has also another value.

I would expect this behaviour if I passed the array by reference but I
do not.

<?php

$array = array(1);

$reference =& $array[0];

echo $array[0], '<br>';
theFunction($array);

echo $array[0], '<br>';

function theFunction($array) {
    $array[0] = 2;
}

?>

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

[2002-12-13 12:00:37] henrik.gebauer <email protected>

I create an array an then a reference to an element of that array.
Then the array is passed to a function (by value!) which changes the
value of the element.
After that, the global array has also another value.

I would expect this behaviour if I passed the array by reference but I
did not.

<?php

$array = array(1);

$reference =& $array[0];

echo $array[0], '<br>';
theFunction($array);

echo $array[0], '<br>';

function theFunction($array) {
    $array[0] = 2;
}

?>

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

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

-- PHP Documentation Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php