foreach() with references broken when iterating only over value
| Bug #27923 | foreach() with references broken when iterating only over value | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2004-04-08 15:20 UTC | Modified: | 2004-04-23 23:46 UTC |
|
||||||||||
| From: | adam at trachtenberg dot com | Assigned: | andi (profile) | |||||||||||
| Status: | Closed | Package: | Scripting Engine problem | |||||||||||
| PHP Version: | 5CVS-2004-04-13 | OS: | * | |||||||||||
| Private report: | No | CVE-ID: | None | |||||||||||
[2004-04-08 15:20 UTC] adam at trachtenberg dot com
Description: ------------ In PHP 5, foreach() allows you to iterate over values by reference. This does not work when you do foreach($array as $value) instead of foreach($array as $key => $value). I believe this patch fixes the problem, but someone should *really* double check it. :) http://www.trachtenberg.com/patches/ foreach_by_reference.txt Reproduce code: --------------- php -r '$array = array(); foreach($array as $value) {)' Expected result: ---------------- Nothing. Actual result: -------------- PHP Fatal error: Key element cannot be a reference in Command line code on line 1
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2004-04-12 19:31 UTC] sniper@php.net
[2004-04-12 19:40 UTC] adam at trachtenberg dot com
Jesus. Sometimes I reduce the test case too far. :) $a = array('foo', 'bar'); foreach ($a as &$value) { $value = strtoupper($value); } print_r($a); $a should have FOO and BAR, as it works okay if you do: foreach ($a as $k => &$value) { $value = strtoupper($value); } But PHP gives an error on the first example w/o my patch. (At least I hope it still does, I need to recompile PHP to triple check.)[2004-04-23 01:44 UTC] amt@php.net
[2004-04-23 20:42 UTC] amt@php.net
[2004-04-23 23:46 UTC] andi@php.net