|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-06-15 18:16 UTC] huf at nws dot hu
Description:
------------
it looks like foreach (var as &val) syntax leaves a refecence alive after the last iteration. calling a subsequent foreach (var as val) (same variable being used to iterate, but not as a ref now), the reference from the first foreach is used to update the array...
Reproduce code:
---------------
<?php
$array = array(1, 2, 3);
foreach ($array as &$a) {
}
unset($a);
var_dump($array);
foreach ($array as $a) {
}
var_dump($array);
Expected result:
----------------
$array unchanged
Actual result:
--------------
$array changed.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 12:00:01 2025 UTC |
code to reproduce should be like: <?php $array = array(1, 2, 3); foreach ($array as &$a) { } var_dump($array); foreach ($array as $a) { } var_dump($array);