Req #33126 [Com]: foreach array pointer behavour is inconsistent on variables vs. references
Edit report at https://bugs.php.net/bug.php?id=33126&edit=1
ID: 33126
Comment by: php at mcq8 dot be
Reported by: php at com dot jkkn dot dk
Summary: foreach array pointer behavour is inconsistent on
variables vs. references
Status: Open
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: FreeBSD
PHP Version: 5.0.4
Block user comment: N
Private report: N
New Comment:
This is fixed since 5.2.1.
Previous Comments:
------------------------------------------------------------------------
[2005-05-25 13:03:16] 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.
------------------------------------------------------------------------
[2005-05-25 11:22:02] 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-24 15:13:54] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=33126&edit=1
Thread (5 messages)