php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28704 strange behaviour with references
Submitted: 2004-06-09 01:34 UTC Modified: 2004-08-15 12:39 UTC
From: spy at spy dot zp dot ua Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.6 OS: FreeBSD-4.x
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: spy at spy dot zp dot ua
New email:
PHP Version: OS:

 

 [2004-06-09 01:34 UTC] spy at spy dot zp dot ua
Description:
------------
As shown in this example, the elements of $a become a references during "for" cycle, although there are not have any aliases to their values.
If we make a copy ($b=$a) of an array, it becomes an array of references too.
And only if we unset the copy ($b), engine founds that $a elements are not really referenced by another vars.

The same result we can get with foreach($a), (because foreach works with a copy of an array as noted in Bug #24486)

BTW, how can I make a really clean COPY of an array, including all of it elements? And it should be useful to have a method to test if variable have a reference(s).

Reproduce code:
---------------
$a = array(1, 2, 3);

for ($i = 0; $i < count($a); $i++) {
    $x =& $a[$i];
}
$x=&$nowhere;

print "Dump 1 =>"; var_dump($a);
$b=$a;
$b['0']=6; $b['1']=7; $b['2']=8;

print "Dump 2 =>"; var_dump($a);

unset($b); 

print "Dump 3 =>"; var_dump($a);



Expected result:
----------------
I don't know what to expect at all now =)

Before today I should expect all dumps like this:
Dump * =>array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}


Actual result:
--------------
Dump 1 =>array(3) {
  [0]=>
  &int(1)
  [1]=>
  &int(2)
  [2]=>
  &int(3)
}
Dump 2 =>array(3) {
  [0]=>
  &int(6)
  [1]=>
  &int(7)
  [2]=>
  &int(8)
}
Dump 3 =>array(3) {
  [0]=>
  int(6)
  [1]=>
  int(7)
  [2]=>
  int(8)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-30 03:43 UTC] moriyoshi@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

See http://bugs.php.net/bug.php?id=20993&edit=3
 [2004-06-30 15:17 UTC] spy at spy dot zp dot ua
Of course, I have checked manual, much more then twice.

Sorry, if you have no enough time to understand, that it is either real bug, or a problem to be discussed and solved. And I have no time to dig into source code to find out causes.
 [2004-08-15 12:39 UTC] et@php.net
http://de.php.net/manual/en/language.references.whatdo.php
"Note:  If array with references is copied, its values are not dereferenced. This is valid also for arrays passed by value to functions."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 15:01:27 2024 UTC