#41603 [Opn->Bgs]: foreach not manipulating last element when passed by reference
| From: | tony2001@php.net | Date: | Wed, 06 Jun 2007 06:15:15 +0000 |
| Subject: | #41603 [Opn->Bgs]: foreach not manipulating last element when passed by reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-113439@lists.php.net to get a copy of this message | ||
ID: 41603
Updated by: tony2001@php.net
Reported By: david at terrainferno dot net
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: Windows XP
PHP Version: 5.2.3
New Comment:
Please search the database before reporting.
Previous Comments:
------------------------------------------------------------------------
[2007-06-06 04:28:21] judas dot iscariote at gmail dot com
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" :-)
------------------------------------------------------------------------
[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