Doc #68457 [Nab]: Reference by pointer inaccurate in foreach
| From: | yohgaki@php.net | Date: | Thu, 20 Nov 2014 01:35:47 +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-11674@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:
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.
Previous Comments:
------------------------------------------------------------------------
[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);
------------------------------------------------------------------------
[2014-11-20 01:20:54] yohgaki@php.net
I got it. I works as it documented ;)
http://3v4l.org/eH1NY
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
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