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:
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
Previous Comments:
------------------------------------------------------------------------
[2011-04-26 12:24:28] [email protected]
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] [email protected]
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.
------------------------------------------------------------------------
[2011-04-26 08:33:18] blacknot at gmail dot com
I have similar problem:
$tags = array('iphone', 'apple', 'iPhone 4');
foreach ($tags as &$v) $v = trim($v);
$tags2 = $tags;
foreach ($tags2 as &$a) $a = '*';
print_r($tags);
Result:
Array
(
[0] => iphone
[1] => apple
[2] => *
)
------------------------------------------------------------------------
[2011-04-08 13:10:27] hitchiker at mail dot ru
I was sure that arrays are passed to functions by value, not by
reference. It looks like by-reference iteration changes array not only
isnide foreach statement, but array remains changed even after
iteration.
------------------------------------------------------------------------
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