#41603 [NEW]: foreach not manipulating last element when passed by reference
| From: | david at terrainferno dot net | Date: | Tue, 05 Jun 2007 22:12:57 +0000 |
| Subject: | #41603 [NEW]: foreach not manipulating last element when passed by reference | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-113431@lists.php.net to get a copy of this message | ||
From: david at terrainferno dot net
Operating system: Windows XP
PHP version: 5.2.3
PHP Bug Type: Arrays related
Bug description: foreach not manipulating last element when passed by reference
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 bug report at http://bugs.php.net/?id=41603&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=41603&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=41603&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=41603&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=41603&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=41603&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=41603&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=41603&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=41603&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=41603&r=support
Expected behavior: http://bugs.php.net/fix.php?id=41603&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=41603&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=41603&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=41603&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=41603&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=41603&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=41603&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=41603&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=41603&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=41603&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=41603&r=mysqlcfg