php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33126 foreach array pointer behavour is inconsistent on variables vs. references
Submitted: 2005-05-24 15:13 UTC Modified: 2015-01-08 23:36 UTC
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:4 of 5 (80.0%)
Same Version:3 (75.0%)
Same OS:3 (75.0%)
From: php at com dot jkkn dot dk Assigned: ajf (profile)
Status: Closed Package: *General Issues
PHP Version: 5.0.4 OS: FreeBSD
Private report: No CVE-ID: None
 [2005-05-24 15:13 UTC] php at com dot jkkn dot dk
Description:
------------
When nesting foreach's on the same variable OR using it together with array_next() gives inconsistent behavour.

When you use foreach() on the same variable within another foreach() you would expect either of two different behavours.

Either:
A) They would share the same array pointer.
B) They would each have their own pointer

Documentation says method A is in use... but not always it seems.

Confirmed on PHP 5.0.4 and PHP 4.3.8 that this behavour is inconsistent.

Below is provided two examples which show that foreach will act in two different ways when used on a by-ref variable or a normal variable.

Reproduce code:
---------------
/ EXAMPLE 1:
$numbers = range(1, 10);
function printNumbers() {
    global $numbers;
    foreach ($numbers as $number) {
        echo $number . ', ';
    }
}

//$numbers =& $numbers; //<---- by reference (var.name does not matter) provides alternative behavour
foreach ($numbers as $number) {
    echo $number . ": ";
    printNumbers();
    echo "\n";
}

// EXAMPLE 2:
$numbers = range(1, 10);
foreach ($numbers as $skip) {
    echo $skip . ',';
    next($numbers);
}
echo "\n";

// alternative behavour when using by-reference
$numbers2 =& $numbers;
foreach ($numbers2 as $skip) {
    echo $skip . ',';
    next($numbers2);
}

Expected result:
----------------
The code should show the same result if iterating over a normal array as iterating over a array-by-reference.

Preferable both loops should show that a foreach has its own array-pointer (method B above) as the documentation clams.

Actual result:
--------------
EXAMPLE 2:
1,2,3,4,5,6,7,8,9,10
1,3,5,7,9

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-05-25 11:22 UTC] sniper@php.net
RTFM: 

"Note:  Unless the array is referenced, 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."

 [2005-05-25 13:03 UTC] php at com dot jkkn dot dk
You are wrong - that somehow explains example 2 but NOT example 1. Notice that the code is IDENTICAL and does not manipulate the internal pointer in any way.

Also RTFM:
"<?php
$a =& $b;
?>  

it means that $a and $b point to the same content. 
Note: $a and $b are completely equal here, that's not $a is pointing to $b or vice versa, that's $a and $b pointing to the same place."

It is highly inconsistent that code using, what should be the exact same input, are behavouring in a different manner.

I suggest this as a feature/change request if it is not seen as a bug.

Also notice that documented bugs also still are bugs.
 [2014-10-26 13:59 UTC] php at mcq8 dot be
This is fixed since 5.2.1.
 [2015-01-08 23:36 UTC] ajf@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: ajf
 [2015-01-08 23:36 UTC] ajf@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Sep 15 03:00:01 2025 UTC