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

From: Date: Fri, 21 Nov 2014 03:03:36 +0000
Subject: Doc #68457 [Nab]: Reference by pointer inaccurate in foreach
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-11686@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 Updated by: yohgaki@php.net Reported by: mberding at dtoolbox dot com Summary: Reference by pointer inaccurate in foreach Status: Not a bug Type: Documentation Problem Package: Variables related Operating System: CentOS PHP Version: 5.4.35 Block user comment: N Private report: N New Comment: @rasmus I agree. We are better to stay in the basic principle. Previous Comments: ------------------------------------------------------------------------ [2014-11-20 02:24:12] rasmus@php.net yohgaki, there is absolutely nothing wrong here. As Adam said, there is no block-scope in PHP and we shouldn't in any way mangle variables in loops. The original code in this report is exactly equivalent to: $a = array(1,2,3); // First loop $y = &$a[0]; $y = $y * 2; $y = &$a[1]; $y = $y * 2; $y = &$a[2]; $y = $y * 2; // Second loop $y = $a[0]; echo $y.', '; $y = $a[1]; echo $y.', '; $y = $a[2]; echo $y.', '; Simply unrolling the loops here. $y must be a reference to $a[2] at the start of the second loop because that is what the code says to do. If we artificially try to separate that reference all sorts of code that is written to use references correctly will break. ------------------------------------------------------------------------ [2014-11-20 02:07:08] gabri dot ns at gmail dot com there is nothing wrong with foreach documentation, it is @mberding lack of understanding of reference that cause this, and i think it is because reference documentation where placed far below. since reference is about variable, why don't place it right after? i think its a good thing. ------------------------------------------------------------------------ [2014-11-20 01:42:42] yohgaki@php.net http://3v4l.org/ui7LJ The last element became usual var with unset(), though. Separate zval after foreach by default, parhaps? ------------------------------------------------------------------------ [2014-11-20 01:35:46] yohgaki@php.net Or is it possible not to change the last element of the input array a reference? It would be the best behavior. http://3v4l.org/QSZXm According this result, all elements became reference until 5.2.0 which is consistent for elements. Current behavior is inconsistent, it seems. ------------------------------------------------------------------------ [2014-11-20 01:27:59] yohgaki@php.net Sample code in the doc may be improved. I didn't understand what's going on at first. http://3v4l.org/jNfvt <?php $a = array(1,2,3,4); foreach($a AS &$y) { $y = $y*2; } echo PHP_EOL; var_dump($a); foreach($a AS $y) { echo $y.' '; } echo PHP_EOL; var_dump($a); ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=68457 -- Edit this bug report at https://bugs.php.net/bug.php?id=68457&edit=1

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