Edit report at http://bugs.php.net/bug.php?id=54408&edit=1
ID: 54408
Updated by: rasmus@php.net
Reported by: hitchiker at mail dot ru
Summary: Unexpected behaviour when using by-reference
iteration
Status: Bogus
Type: Bug
Package: Arrays related
Operating System: Win 7 x64, 2.6.18
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
I don't understand why you don't expect references to be copied.
So, if you had code like this:
<?php
$a = 1;
$b = 2;
$c = 3;
$arr = array(&$a, $b, &$c);
$arr2 = $arr;
var_dump($arr2);
You are expecting the $a and $c references to get dropped
in $arr2?
They are not dropped. It is a by-value copy, and the by-value
copy of a reference remains a reference.
Previous Comments:
------------------------------------------------------------------------
[2011-04-26 17:12:37] blacknot at gmail dot com
In this scope is true. Variable $v must be reference, but we copy array
($tags2 = $tags;) and the last element is still reference. It is
unexpected!
------------------------------------------------------------------------
[2011-04-26 16:35:55] rasmus@php.net
It is completely consistent though. You are moving that reference along
in the foreach loop. It would be inconsistent if we magically unset it.
It's not that different from something like:
for($i=0; $i<=10; $i++) echo $a[$i];
You don't really think about it, but after this loop $i is going to be
11. What you are suggesting is that either $i shouldn't be set anymore,
or that it somehow shouldn't be set to 11. It is more obvious in this
case, so perhaps it isn't unexpected for you, but it really is the same
thing.
------------------------------------------------------------------------
[2011-04-26 12:39:19] blacknot at gmail dot com
thanks Johannes, after articles I read interesting hack about unsetting
referense value: "Standard practise after any loop iteration when using
references should be an unset()"
But it is hack and unexpected behaviour
------------------------------------------------------------------------
[2011-04-26 12:24:28] johannes@php.net
I once tried to come up with some text and pictures explaining this:
http://schlueters.de/blog/archives/141-References-and-foreach.html
------------------------------------------------------------------------
[2011-04-26 10:25:26] blacknot at gmail dot com
Why only last element of array have reference link? It`s realy
unexpected behaviour. We copy array $tags2 = $tags; but only last
element have reference.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/bug.php?id=54408--
Edit this bug report at http://bugs.php.net/bug.php?id=54408&edit=1