#49752 [Opn]: reference maintained across different loops

From: Date: Fri, 02 Oct 2009 21:31:51 +0000
Subject: #49752 [Opn]: reference maintained across different loops
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-143204@lists.php.net to get a copy of this message
ID: 49752 Updated by: fa@php.net Reported By: stevew at amainhobbies dot com Status: Open Bug Type: Variables related Operating System: Win32 / Linux PHP Version: 5.2.11 New Comment: This has been like this since at least 5.1.6 (just tested) and also in 5.3.0. Previous Comments: ------------------------------------------------------------------------ [2009-10-02 18:24:18] stevew at amainhobbies dot com Description: ------------ I don't expect the $total in the second foreach to still be a reference to the last item from the previous foreach. Reproduce code: --------------- <?php $totals = array(array('class' => 'Foo', 'value' => '1'), array('class' => 'Test', 'value' => '2'), array('class' => 'Bar', 'value' => '3')); foreach ($totals as &$total) { switch ($total['class']) { case 'Test': $total['value'] *= 2; break; default: break; } } foreach ($totals as $total) { echo $total['class'] . ' : ' . $total['value'] . "\n"; } ?> Expected result: ---------------- Foo : 1 Test : 4 Bar : 3 Actual result: -------------- Foo : 1 Test : 4 Test : 4 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=49752&edit=1

« previous php.bugs (#143204) next »