php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48137 assigning array element reference appears to modify array
Submitted: 2009-05-03 17:01 UTC Modified: 2009-05-04 02:21 UTC
From: mike at clove dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.9 OS: Mac OS X 10.5.6
Private report: No CVE-ID: None
 [2009-05-03 17:01 UTC] mike at clove dot com
Description:
------------
Assigning reference to an array element appears to modify the
array element rather than simply creating a reference to it.
This is surprising, to say the least.

At least I think this is a bug and couldn't find anything with the same behavior and such minimal code.


Reproduce code:
---------------
$a = array(1,2);
$r =& $a[0];
var_dump($a);


Expected result:
----------------
array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}


Actual result:
--------------
array(2) {
  [0]=>
  &int(1)
  [1]=>
  int(2)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-03 17:04 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Nah, this is fine. It doesn\'t modify the array. The & just shows that the is_ref bit on the zval container that contains the int(1) value is set.
 [2009-05-04 00:34 UTC] scottmac@php.net
References aren't pointers, all they say is that the two values should point to the same content. This is still the case even if the array was changed.

http://www.php.net/manual/en/language.references.whatare.php
 [2009-05-04 02:21 UTC] mike at clove dot com
Thanks derick at php dot net.

It's confusing to have var_dump() tell me that the assignment has changed element 0 from a value to a reference. That seems to me to be a bug in var_dump(): in the code, $r is the reference, not $a[0] - which should still be a value. I think this means var_dump() is displaying unreliable information.

to scottmac at php dot net: the issue isn't what a reference is, it's that the a := b appears to be changing the value of 'b', which is just plain wrong for any assignment in any programming language I know [unless $a =& $b; $a = $b; which would be specifically asking to do this].
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun May 24 20:00:01 2026 UTC