#48561 [Opn]: foreach (var as &var construct overwrites last element of array
| From: | huf at nws dot hu | Date: | Mon, 15 Jun 2009 18:20:23 +0000 |
| Subject: | #48561 [Opn]: foreach (var as &var construct overwrites last element of array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-138331@lists.php.net to get a copy of this message | ||
ID: 48561
User updated by: huf at nws dot hu
Reported By: huf at nws dot hu
Status: Open
Bug Type: Arrays related
Operating System: linux 2.6
PHP Version: 5.2.9
New Comment:
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);
Previous Comments:
------------------------------------------------------------------------
[2009-06-15 18:16:33] 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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=48561&edit=1