php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48875 unexpected foreach behavior with references, 2 foreaches, and same value var
Submitted: 2009-07-10 00:36 UTC Modified: 2009-07-10 18:12 UTC
From: stepheneliotdewey at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.10 OS: Vista 64-bit
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: stepheneliotdewey at gmail dot com
New email:
PHP Version: OS:

 

 [2009-07-10 00:36 UTC] stepheneliotdewey at gmail dot com
Description:
------------
If you look at the final print-out for the below, it will print a, b, b, instead of the expected a, b, c. I don't think this is the expected behavior.

Reproduce code:
---------------
<?php
    $a = array('a', 'b','c');
    foreach($a as &$row){
        //you don't have to do anything here
       }
    print_r($a);
    foreach($a as $row){
       echo "<br />".$row;
    }
?>

Expected result:
----------------
a
b
c

Actual result:
--------------
a
b
b

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-10 00:40 UTC] stepheneliotdewey at gmail dot com
I should note that I first saw this bug in a comment posted earlier today in the documentation:

http://us3.php.net/manual/en/control-structures.foreach.php#92116
 [2009-07-10 18:12 UTC] jani@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

Please, read the manual page too. This is documented there.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Dec 04 18:00:01 2025 UTC