Bug #63752 [Opn->Nab]: foreach increments pointer used by current() once
Edit report at https://bugs.php.net/bug.php?id=63752&edit=1
ID: 63752
Updated by: laruence@php.net
Reported by: terrafrost@php.net
Summary: foreach increments pointer used by current() once
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Windows 7
PHP Version: 5.4.9
Block user comment: N
Private report: N
New Comment:
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
1. at the beginning of foreach: ZEND_FE_RESET which will increase the refoucnt
of $a
2. then FE_FETCH, which will increase internal pointer of $a
3. then current, current declared to accept a reference, but $a is not a ref and
refcount > 1 , then -> separation
hope I made it clear.
thanks
Previous Comments:
------------------------------------------------------------------------
[2012-12-12 15:06:01] terrafrost@php.net
Description:
------------
foreach seems to increment the pointer used by current() once even if foreach
loops multiple times. Seems to me that it should not increment it at all or
increment it multiple times. Not once and that's it.
Test script:
---------------
<?php
$a = array(1, 2, 3, 4, 5);
foreach ($a as $key => $elem) {
echo "$key = $elem"; echo ' = ';
var_dump(current($a));
}
?>
Expected result:
----------------
0 = 1 = int(1)
1 = 2 = int(1)
2 = 3 = int(1)
3 = 4 = int(1)
4 = 5 = int(1)
Actual result:
--------------
0 = 1 = int(2)
1 = 2 = int(2)
2 = 3 = int(2)
3 = 4 = int(2)
4 = 5 = int(2)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63752&edit=1
Thread (2 messages)