Bug->Doc #68457 [Nab]: Reference by pointer inaccurate in foreach

From: Date: Wed, 19 Nov 2014 22:50:10 +0000
Subject: Bug->Doc #68457 [Nab]: Reference by pointer inaccurate in foreach
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-11669@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68457&edit=1 ID: 68457 User updated by: mberding at dtoolbox dot com Reported by: mberding at dtoolbox dot com Summary: Reference by pointer inaccurate in foreach Status: Not a bug -Type: Bug +Type: Documentation Problem Package: Variables related Operating System: CentOS PHP Version: 5.4.35 Block user comment: N Private report: N New Comment: If it's not considered a bug, then the documentation should at least be updated to state that the foreach method does not initiate a new variable for the value variable instead of saying, "Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset()." Simply stating that it still exists doesn't also imply that foreach will not use a new variable when the same variable name is used in a new foreach loop. Previous Comments: ------------------------------------------------------------------------ [2014-11-19 22:32:33] rasmus@php.net http://php.net/foreach ------------------------------------------------------------------------ [2014-11-19 22:28:30] mberding at dtoolbox dot com Description: ------------ In the code below the last element in the $a array is replaced with a copy of the second-to-last element. Tested and confirmed in PHP versions: 5.3.3, 5.4.34, 5.6.2 Tested OS: CentOS 6.5, 7 Test script: --------------- //Test Script: //------------ $a = array(1,2,3); foreach($a AS &$y) { $y = $y*2; } foreach($a AS $y) { echo $y.', '; } Expected result: ---------------- 2,4,6, Actual result: -------------- 2,4,4, ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68457&edit=1

« previous php.doc.bugs (#11669) next »