php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24539 key() in for loop "breaks" ?
Submitted: 2003-07-08 09:53 UTC Modified: 2003-07-08 10:04 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: black at scene-si dot org Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.2 OS: linux debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: black at scene-si dot org
New email:
PHP Version: OS:

 

 [2003-07-08 09:53 UTC] black at scene-si dot org
Description:
------------
replace key() with current() and you get the value with no problems, key() however doesnt work.

the wierd part, sometimes the bellow code works, since i have 2 "identical" classes, one of which uses this method with key() - and makes correct results, however this short reproducable script doesnt.

works: http://sunshine.krneki.org/files/halcyon/hal.phps
doesnt: http://sunshine.krneki.org/files/halcyon/hal2.phps

(search for 'reset', note that i have changed to use current in hal2.phps in the update function, so check out plot() which is still identical).

cleaning up code is sometimes a b***h ;)

Reproduce code:
---------------
$data = array("foo","bar","x","y");
for (reset($data); $id=key($data); next($data)) {
  var_dump($id);
}
for (reset($data); $id=current($data); next($data)) {
  var_dump($id);
}


Expected result:
----------------
int(0)
int(1)
int(2)
int(3)
string(3) "foo"
string(3) "bar"
string(1) "x"
string(1) "y"


Actual result:
--------------
string(3) "foo"
string(3) "bar"
string(1) "x"
string(1) "y"


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-08 09:58 UTC] sniper@php.net
<_Wez_> your for loop is equivalent to this:
<_Wez_> reset($data);
<_Wez_> while ($id = key($data)) {
<_Wez_> var_dump($id);
<_Wez_> next($data);
<_Wez_> }
<black|one> ... your point?
<_Wez_> $id = key($data) ==> 0 on the first iteration
<_Wez_> while (0) {
<_Wez_> }
 [2003-07-08 10:04 UTC] black at scene-si dot org
[17:00:10] <black|one> damn
[17:00:11] <black|one> i suck
[17:00:16] * black|one repents
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 01:01:32 2025 UTC