|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits              [2013-06-13 21:00 UTC] aharvey@php.net
 
-Status: Open
+Status: Not a bug
  [2013-06-13 21:00 UTC] aharvey@php.net
 | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 09:00:01 2025 UTC | 
Description: ------------ After using same name in first and third foreach script returns wrong result. Last element is changed to one just before it. As we can see it only happens if name of reference and variable used later in foreach is the same("product"). What is worse it affects every next iteration. After that it doesn't matter what is the name of helper variable. Problem affects PHP versions: 5.2, 5.3 and 5.4 at least. Different compilations by different people and companies. Possibly related to: https://bugs.php.net/bug.php?id=60450&edit=2 Test script: --------------- $products = array( array('id'=>1,'name'=>'Cheese'), array('id'=>2,'name'=>'Cake'), array('id'=>3,'name'=>'Fish'), array('id'=>4,'name'=>'Soup'), array('id'=>5,'name'=>'Pasta'), ); $i = 0; foreach($products as &$product) { $product['name'] = $product['name'].$i; $i++; } foreach($products as $p) { echo $p['name']; echo "\n"; } echo "\n"; foreach($products as $product) { echo $product['name']; echo "\n"; } echo "\n"; foreach($products as $p) { echo $p['name']; echo "\n"; } Expected result: ---------------- Cheese0 Cake1 Fish2 Soup3 Pasta4 Cheese0 Cake1 Fish2 Soup3 Pasta4 Cheese0 Cake1 Fish2 Soup3 Pasta4 Actual result: -------------- Cheese0 Cake1 Fish2 Soup3 Pasta4 Cheese0 Cake1 Fish2 Soup3 Soup3 Cheese0 Cake1 Fish2 Soup3 Soup3