Doc #68457 [Nab]: Reference by pointer inaccurate in foreach
| From: | yohgaki@php.net | Date: | Thu, 20 Nov 2014 01:20:54 +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-11672@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:
I got it. I works as it documented ;)
http://3v4l.org/eH1NY
Previous Comments:
------------------------------------------------------------------------
[2014-11-19 23:15:49] yohgaki@php.net
I'm missing something probably, but this result is interesting to me.
http://3v4l.org/G2fXg
http://3v4l.org/CCadi
I don't have time to look into now :)
------------------------------------------------------------------------
[2014-11-19 23:00:02] aharvey@php.net
PHP doesn't have block scope in general, so I'm not sure why it would be expected that $y
would be bound to the scope of the foreach loop in any way.
I'm not really inclined to change the text on the foreach page yet again, honestly
â it's been through so many revisions at this point that it's getting a little
silly, and this variant seems to have generated fewer bug reports than the previous ones.
------------------------------------------------------------------------
[2014-11-19 22:50:09] mberding at dtoolbox dot com
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.
------------------------------------------------------------------------
[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