From: bugreporter at to dot mabomuja dot de
Operating system: Linux/Windows
PHP version: 5.2.4
PHP Bug Type: *General Issues
Bug description: foreach modifies array pointer
Description:
------------
In the code example below, when entering the for-loop, the arraypointer is
at position 1 (the 2nd element).
When you comment out the foreach-loop before OR insert the reset-line,
output starts - as expected - at the first array element (posistion 0).
This means "foreach" modifies the array pointer, although the
documentation says "foreach operates on a copy of the specified array and
not the array itself. Therefore, the array pointer is not modified"
Testing with versions 4.4.7/5.2.3/5.3dev(2007-11-08-Snap) of Windows zip
package and 4.4.7/5.2.1 on gentoo (5.2.3 not testet on linux) shows correct
behavior.
Reproduce code:
---------------
$t=range('a','c');
foreach ($t AS $key => $profil){
$t[$key]="1";
}
//reset($t);
for ($i=0;$i<sizeof($t);$i++){
echo key($t) . "->".current($t)."\n";
next ($t);
}
print_r($t);
Expected result:
----------------
version 5.2.3:
0->1
1->1
2->1
Array
(
[0] => 1
[1] => 1
[2] => 1
)
Actual result:
--------------
version 5.2.4:
1->1
2->1
->
Array
(
[0] => 1
[1] => 1
[2] => 1
)
--
Edit bug report at http://bugs.php.net/?id=43220&edit=1
--
Try a CVS snapshot (PHP 4.4):
http://bugs.php.net/fix.php?id=43220&r=trysnapshot44
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=43220&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=43220&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=43220&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=43220&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=43220&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=43220&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=43220&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=43220&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=43220&r=support
Expected behavior: http://bugs.php.net/fix.php?id=43220&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=43220&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=43220&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=43220&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=43220&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=43220&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=43220&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=43220&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=43220&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=43220&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=43220&r=mysqlcfg