Bug #64066 [Opn->Nab]: foreach with reference alter variable
Edit report at https://bugs.php.net/bug.php?id=64066&edit=1
ID: 64066
Updated by: tyrael@php.net
Reported by: w at ndrille dot fr
Summary: foreach with reference alter variable
-Status: Open
+Status: Not a bug
Type: Bug
Package: Testing related
Operating System: Linux and Windows
PHP Version: 5.4.11
Block user comment: N
Private report: N
New Comment:
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
It is a documented behavior:
http://php.net/manual/en/control-structures.foreach.php
"Reference of a $value and the last array element remain even after the foreach
loop. It is recommended to destroy it by unset()."
Previous Comments:
------------------------------------------------------------------------
[2013-01-24 23:49:05] w at ndrille dot fr
Description:
------------
When we use foreach with reference and use foreach again with the same variable
without doing any action, php will alter array: The last value is doubled
Test script:
---------------
<?php
$a = array(1,2,3);
$z = $a;
foreach ($a as &$b){}
foreach ($a as $b){}
print_r($a);
if ($a != $z)
print('bug');
?>
Expected result:
----------------
Array ( [0] => 1 [1] => 2 [2] => 3 )
Actual result:
--------------
Array ( [0] => 1 [1] => 2 [2] => 2 ) bug
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64066&edit=1
Thread (2 messages)