Bug #76931 [Com]: foreach by reference copies last array element from second-last
| From: | spam2 at rhsoft dot net | Date: | Mon, 24 Sep 2018 22:27:15 +0000 |
| Subject: | Bug #76931 [Com]: foreach by reference copies last array element from second-last | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-217230@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76931&edit=1
ID: 76931
Comment by: spam2 at rhsoft dot net
Reported by: kellnerdenes at gmail dot com
Summary: foreach by reference copies last array element from
second-last
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Win64
PHP Version: 7.1.22
Block user comment: N
Private report: N
New Comment:
> second foreach should create a new variable as $x, not reuse it.
> I can't see why it would keep it.
and I don't see how PHP sould smell your intention and create magically a new variable - what
if i want to re-use the referenced varible in the next step - again: don't use references until
you fully understand them
Previous Comments:
------------------------------------------------------------------------
[2018-09-24 22:02:58] requinix@php.net
> the second foreach should create a new variable as $x
A variable is a variable. When it is created it exists until it drops out of scope or something
unset()s it. There is no such thing as creating a "new" variable with an existing name -
it's merely assigning a new value to it.
If you literally wanted a new variable then you should literally use a new variable. Like $y.
------------------------------------------------------------------------
[2018-09-24 21:57:17] kellnerdenes at gmail dot com
Sure, you're right, it's somewhat expectable that the reference remains in $x from the
first foreach; but the second foreach should create a new variable as $x, not reuse it. I can't
see why it would keep it.
------------------------------------------------------------------------
[2018-09-24 21:50:27] spam2 at rhsoft dot net
to be clear:
foreach($a as &$x);
unset($x);
foreach($a as $x);
just don't use references until you understand the side-effects and are really sure you ebenfit
from it which is motsly not true given the copy-on-write nature of PHP
------------------------------------------------------------------------
[2018-09-24 21:48:28] spam2 at rhsoft dot net
man uset()
------------------------------------------------------------------------
[2018-09-24 21:26:25] requinix@php.net
http://php.net/manual/en/control-structures.foreach.php
Big red warning. Should be hard to miss.
------------------------------------------------------------------------
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=76931
--
Edit this bug report at https://bugs.php.net/bug.php?id=76931&edit=1