php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #20993 Element value changes without asking
Submitted: 2002-12-13 12:00 UTC Modified: 2004-07-26 17:16 UTC
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (33.3%)
From: henrik dot gebauer at web dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0CVS-2002-12-13 OS: Any
Private report: No CVE-ID:
 [2002-12-13 12:00 UTC] henrik dot gebauer at web dot de
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;
}

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-13 12:42 UTC] msopacua@php.net
Verified and added testcase to CVS
 [2002-12-13 12:50 UTC] moriyoshi@php.net
Verified with 4.2.3

 [2002-12-13 14:51 UTC] moriyoshi@php.net
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-18 03:25 UTC] msopacua@php.net
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.
 [2004-04-16 22:59 UTC] php dot devel at homelinkcs dot com
I don't wish to generate unnecessary traffic, however, I    
have been unable to find any reference to this issue in    
the current documentation at http://www.php.net/manual.     
Since, it's been well over a year since this issue (and    
several related bug reports) has been brought up, can    
anyone give me an update on this it's status?   I consider    
this a serious problem because, IMHO, the behavior seems    
illogical from a high-level view of references and copies    
(cf (from chapter 7 of the manual): "[A]fter assigning one    
variable's value to another, changing one of those    
variables will have no effect on the other.").   For that    
reason, furthermore, lack of knowedge of this behavior    
can make code affected by it quite difficult to debug.
 [2004-04-17 17:59 UTC] nlopess@php.net
As you may have noted, we have more than 100 open bugs, so its hard to response quickly.
However, if you want to submit a patch to the documentation, we are open to contributions.
 [2004-04-22 01:34 UTC] php dot devel at homelinkcs dot com
This is my best effort (with the help of a friend), to 
document this issue.  I have based it on my personal tests  
as well as information from this and related bug reports, 
but I assume it goes without saying that this patch should  
be reviewed by a developer with knowledge in this area 
before being committed.  Thanks for a great programing 
language! 
 
 
Index: references.xml 
=================================================================== 
RCS file: /repository/phpdoc/en/language/references.xml,v 
retrieving revision 1.27 
diff -u -r1.27 references.xml 
--- references.xml	2003/12/21 15:37:29	1.27 
+++ references.xml      2004/04/21 19:20:39 
@@ -43,6 +43,21 @@ 
      </para> 
     </note> 
    </para> 
+   <warning> 
+    <simpara> 
+     Due to peculiarities of the internal workings of 
PHP, if a reference 
+     is made to a single element of an array and then the 
array is copied, 
+     whether by assignment or when passed by value in a 
function call, 
+     the reference is copied as part of the array.  This 
means that 
+     changes to any such elements in either array will be 
duplicated in 
+     the other array (and in the other references), even 
if the arrays 
+     have different scopes (e.g. one is an argument 
inside a function and 
+     the other is global)!  Elements that did not have 
references at the 
+     time of the copy, as well as references assigned to 
those other 
+     elements after the copy of the array, will behave 
normally (i.e. 
+     independent of the other array). 
+    </simpara> 
+   </warning> 
    <para> 
    The same syntax can be used with functions, that 
return references, 
    and with <literal>new</literal> operator (in PHP 4.0.4 
and later):
 [2004-04-25 20:32 UTC] nlopess@php.net
Looks good. any objection?
 [2004-05-03 23:46 UTC] php dot devel at homelinkcs dot com
I don't have any objection. :-D
 [2004-07-26 17:16 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 25 07:02:14 2014 UTC