#33126 [NEW]: foreach array pointer behavour is inconsistent on variables vs. references
| From: | php at com dot jkkn dot dk | Date: | Tue, 24 May 2005 13:13:55 +0000 |
| Subject: | #33126 [NEW]: foreach array pointer behavour is inconsistent on variables vs. references | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-79443@lists.php.net to get a copy of this message | ||
From: php at com dot jkkn dot dk
Operating system: FreeBSD
PHP version: 5.0.4
PHP Bug Type: Arrays related
Bug description: foreach array pointer behavour is inconsistent on variables vs. references
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 bug report at http://bugs.php.net/?id=33126&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=33126&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=33126&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=33126&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=33126&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=33126&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=33126&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=33126&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=33126&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=33126&r=support
Expected behavior: http://bugs.php.net/fix.php?id=33126&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=33126&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=33126&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=33126&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=33126&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=33126&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=33126&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=33126&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=33126&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=33126&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=33126&r=mysqlcfg