php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28939 Invalid array internal pointer behavior in foreach statement
Submitted: 2004-06-28 09:56 UTC Modified: 2005-02-15 01:30 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: tomas_matousek at hotmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0RC3 OS: WinXP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tomas_matousek at hotmail dot com
New email:
PHP Version: OS:

 

 [2004-06-28 09:56 UTC] tomas_matousek at hotmail dot com
Description:
------------
Array's internal pointer (which one can manipulate by next, prev, current, etc. functions) has invalid bahavior in foreach statement. Moreover, there is a contradictional description of this behavior in the manual.

from manual:

"Note: 
Also note that foreach operates on a copy of the specified array and not the array itself. Therefore, the ARRAY POINTER IS NOT MODIFIED as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the INTERNAL POINTER OF THE ORIGINAL ARRAY IS ADVANCED with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array. 
"

It is not clear whether or not the internal pointer of the original array is advanced during enumeration.

I made some tests and it seems to me that foreach statement mishandles array's internal pointer (see "reproducable code").

I would prefer if the original array's pointer was not influenced by foreach statement because one can do changes in the array during enumeration (which is legal since it is assumed that foreach operates on a copy) and IMHO any changes to the pointer in original array would necessarily lead to strange behavior.



Reproduce code:
---------------
    echo "-- a --\n";

    $a = array(0,1,2);
    
    var_dump(current($a));
    
    foreach ($a as $value)
    {
      /* do not touch $a */
    }
    
    var_dump(current($a));

    $b = array(0,1,2);
    
    echo "-- b --\n";
    
    var_dump(current($b));
    
    foreach ($b as $value)
    {
      var_dump(current($b)); 
    }
    
    var_dump(current($b));


Expected result:
----------------
either the following (pointer is advanced):

-- a --
int(0)
bool(false)
-- b --
int(0)
int(0)
int(1)
int(2)
int(false)

or the following (pointer is not avanced):

-- a --
int(0)
int(0)
-- b --
int(0)
int(0)
int(0)
int(0)
int(0)


Actual result:
--------------
-- a --
int(0)
bool(false)
-- b --
int(0)
int(0)
int(0)
int(0)
int(0)



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-15 01:30 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

"Also note that foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array."
(c) http://php.net/manual/en/control-structures.foreach.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 17:01:33 2024 UTC