Doc #68457 [Nab]: Reference by pointer inaccurate in foreach
| From: | yohgaki@php.net | Date: | Thu, 20 Nov 2014 01:42:42 +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-11675@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:
http://3v4l.org/ui7LJ
The last element became usual var with unset(), though. Separate zval after foreach by default,
parhaps?
Previous Comments:
------------------------------------------------------------------------
[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);
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
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