|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
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
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:2 (66.7%)
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
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. :)

Reproduce code:
php -r '$array = array(); foreach($array as $value) {)'

Expected result:

Actual result:
PHP Fatal error:  Key element cannot be a reference in 
Command line code on line 1


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-12 19:31 UTC]
your example doesn't have any references in it..? (and it works fine when I replace the last ) with } :)

 [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);


$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 

(At least I hope it still does, I need to recompile PHP 
to triple check.)
 [2004-04-12 19:43 UTC]
See also bug #27964 (a bit different case but same errors :)

 [2004-04-23 01:44 UTC]
Any chance of this getting into RC2?
 [2004-04-23 20:42 UTC]
Cannot reproduce on Linux or Mac OS X with snapshots, 
but bug continues to exist on Mac OS X when building 
from CVS.
 [2004-04-23 23:46 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Feb 27 14:01:32 2024 UTC