php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53725 array was changed
Submitted: 2011-01-12 14:20 UTC Modified: 2011-01-13 03:25 UTC
From: Nikolay dot Syrisko at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.17 OS: windows 7
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: Nikolay dot Syrisko at gmail dot com
New email:
PHP Version: OS:

 

 [2011-01-12 14:20 UTC] Nikolay dot Syrisko at gmail dot com
Description:
------------
when I declare array and make foreach 

step 1: foreach ($list as &$rec) {

then make

step 2: foreach ($list as $rec) {

and last element of $list at the moment "step 1" will broken after executing of "step 2" with data of last element of $list at the moment "step 2"

ps: checked under different versions including 5.2.17

when you make unset($rec) before calling "step 2" bug will not appear.

Test script:
---------------
$list = array(1, 2, 3, 4, 5);
foreach($list as &$rec);
echo '<pre>'.print_r($list, 1).'</pre>';
$list[] = 6;
$list[] = 7;
foreach($list as $rec);
echo '<pre>'.print_r($list, 1).'</pre>';


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-13 03:25 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2011-01-13 03:25 UTC] aharvey@php.net
This is bizarre, but documented behaviour, and is why the foreach 
manual page recommends calling unset() on the loop variable when
using foreach in assign by reference mode.

Closing.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC