php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46279 next() behaviour inside foreach changed
Submitted: 2008-10-12 11:14 UTC Modified: 2008-10-13 09:44 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: jozefchutka at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.6 OS: windows
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: jozefchutka at gmail dot com
New email:
PHP Version: OS:

 

 [2008-10-12 11:14 UTC] jozefchutka at gmail dot com
Description:
------------
there is a bug in 5.2.6 causing unexpected results when using next() function indisde foreach. Results from php 5.1, or 4.x are as expected.

Reproduce code:
---------------
<?php
$arr=array('a','b','c', 'd','e');
foreach($arr as $a){
	print_r($a);
	echo " next is: ";
	print_r(next($arr));
	echo "<br />";
}
?>

Expected result:
----------------
a next is: b
b next is: c
c next is: d
d next is: e
e next is: 

Actual result:
--------------
a next is: c
b next is: d
c next is: e
d next is:
e next is: 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-13 09:44 UTC] tony2001@php.net
Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach  has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it. 

http://php.net/foreach
 [2010-07-29 16:13 UTC] dalonso at sistemio dot com
it also happens the same with each method () with each() method
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 25 13:00:03 2025 UTC