php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63752 foreach increments pointer used by current() once
Submitted: 2012-12-12 15:06 UTC Modified: 2012-12-13 08:34 UTC
From: terrafrost@php.net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.9 OS: Windows 7
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: terrafrost@php.net
New email:
PHP Version: OS:

 

 [2012-12-12 15:06 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-13 08:34 UTC] laruence@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

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
 [2012-12-13 08:34 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC