#41603 [Com]: foreach not manipulating last element when passed by reference
| From: | judas dot iscariote at gmail dot com | Date: | Wed, 06 Jun 2007 04:28:22 +0000 |
| Subject: | #41603 [Com]: foreach not manipulating last element when passed by reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-113437@lists.php.net to get a copy of this message | ||
ID: 41603
Comment by: judas dot iscariote at gmail dot com
Reported By: david at terrainferno dot net
Status: Open
Bug Type: Arrays related
Operating System: Windows XP
PHP Version: 5.2.3
New Comment:
This is the expected beahviuor, this issue has been reported counteless
of time, and explained to death.
for an explanation see comment by gardan at gmx dot de here:
http://bugs.php.net/bug.php?id=29992
I wonder why does not raise an E_NOTICE "shooting yourself in the foot
with references" :-)
Previous Comments:
------------------------------------------------------------------------
[2007-06-05 22:12:56] david at terrainferno dot net
Description:
------------
This was reported in 2006 in the foreach user comment
http://uk.php.net/manual/en/control-structures.foreach.php#60806,
but
using php5 using references in a foreach loop, manipulating the last
element in an array seems to have no effect, the reference is referred
to as $v.
Reproduce code:
---------------
// fails to work as expected
$ar = array(1,2,3,4);
foreach ($ar as &$v){
$v *= 2;
}
foreach($ar as $v){
echo $v . '<br>';
}
// works correctly
$ar = array(1,2,3,4);
foreach ($ar as &$o){
$o *= 2;
}
foreach($ar as $o){
echo $o . '<br>';
}
Expected result:
----------------
2
4
6
8
Actual result:
--------------
2
4
6
6
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=41603&edit=1