From: plasticlobster at gmail dot com
Operating system: Debian
PHP version: 5.2.11
PHP Bug Type: Arrays related
Bug description: foreach by reference followed by foreach not by reference -
Unexpected Results
Description:
------------
Running two foreach loops on different parts of the same array, one by
reference, one not causes unexpected results.
Reproduce code:
---------------
$foo = array(1,2,3,4,5);
foreach ($foo as $key => &$val) {
$val++;
}
foreach ($foo as $key => $val) {
echo $val;
}
Expected result:
----------------
Expected result is:
23456
Actual result:
--------------
Actual Result is:
23455
This is duplicate of bug #47388 which was dismissed without investigation.
The problem here is two-fold:
1. The scope of foreach should not extend beyond a foreach loop.
2. $val should be re-initialized as a VALUE, not a REFERENCE.
It looks like the second foreach is doing something comparable to $val =
$foo[0]. It should be calling unset($val) prior to assigning anything to
it. This is like going into a for loop and finding out that your explicit
value for the incrementor variable didn't get set because you had used it
in a previous for loop.
I would have commented on #47388, but comments were disabled as it was
abruptly closed.
When deciding whether or not to accept this as a bug, please keep in mind
that the current implementation serves no programmatic purpose and that the
proposed implementation is not only more intuitive/expected, but also
serves a purpose.
--
Edit bug report at http://bugs.php.net/?id=50485&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=50485&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=50485&r=trysnapshot53
Try a snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=50485&r=trysnapshot60
Fixed in SVN:
http://bugs.php.net/fix.php?id=50485&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=50485&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=50485&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=50485&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=50485&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=50485&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=50485&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=50485&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=50485&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=50485&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=50485&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50485&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=50485&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=50485&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=50485&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=50485&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=50485&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=50485&r=mysqlcfg