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

 ID:                 54408
 Comment by:         blacknot at gmail dot com
 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:

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!


Previous Comments:
------------------------------------------------------------------------
[2011-04-26 16:35:55] ras...@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] johan...@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.

------------------------------------------------------------------------
[2011-04-26 08:40:56] ras...@php.net

There is no expression-scope in PHP. If you create references in an
array in a foreach loop or in any other expression, those references
will still exist after the foreach. Follow the references and you will
see that the output makes sense. There is no bug here.

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


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

Reply via email to