php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47028 Strange foreach behavior when using references
Submitted: 2009-01-07 14:33 UTC Modified: 2009-02-10 11:00 UTC
Votes:8
Avg. Score:5.0 ± 0.0
Reproduced:7 of 8 (87.5%)
Same Version:6 (85.7%)
Same OS:7 (100.0%)
From: kobieta dot ryba at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 6CVS-2009-01-07 (CVS) OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kobieta dot ryba at gmail dot com
New email:
PHP Version: OS:

 

 [2009-01-07 14:33 UTC] kobieta dot ryba at gmail dot com
Description:
------------
I've noticed strange behavior when using references and foreach loop. 
It throws an error "Invalid argument supplied for foreach" but if the code
// $ass = &$array;
is commented out, the result is as expected:
Output:
in
in
in


Reproduce code:
---------------
<?
$array = array("1", "2", "3");
$ass = &$array;
foreach($array as $k => &$v)
{
  $array = false;

  $v = "Test";
  echo "in\n";
}
?>

Expected result:
----------------
in
in
in


Actual result:
--------------
in

Warning: Invalid argument supplied for foreach() in test.php on line 5


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-15 10:12 UTC] kobieta dot ryba at gmail dot com
Is there anybody reading this?
 [2009-02-10 11:00 UTC] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You overwrite the variable you're looping through, no wonder the second iteration fails.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 06 17:01:33 2025 UTC