php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49386 foreach with reference causes funny behaviour
Submitted: 2009-08-27 11:38 UTC Modified: 2009-08-27 11:41 UTC
From: ingo dot aengenheister at gmx dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0 OS: Windows Vista
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: ingo dot aengenheister at gmx dot de
New email:
PHP Version: OS:

 

 [2009-08-27 11:38 UTC] ingo dot aengenheister at gmx dot de
Description:
------------
is it a bug or a feature ?

Reproduce code:
---------------
<?php

$a = array('a','b','c','d','e');
foreach($a as &$b){}

foreach($a as $b){
    echo $b; // last element lost in space, next to last element appears 2 times
}

?>

Expected result:
----------------
abcde

Actual result:
--------------
abcdd

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-27 11:41 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

foreach by ref will not delete the reference after foreach is finished, meaning $b is still a ref to the last element after the first foreach, which you then overwrite with the values of the second array.
 [2011-11-27 11:32 UTC] snapwilliam at gmail dot com
Where can I find the explanation as to why this is not a bug?
 [2011-11-27 11:55 UTC] snapwilliam at gmail dot com
Excuse my above comment. Got confused.
 [2015-01-01 20:23 UTC] chealer at gmail dot com
The problematic behavior behind this issue is being tracked in #62132.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 02:01:28 2024 UTC