|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48897 wrong count() behaviour
Submitted: 2009-07-12 21:33 UTC Modified: 2009-07-12 21:57 UTC
From: allyouneedis at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.0 OS: Windows7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: allyouneedis at gmail dot com
New email:
PHP Version: OS:


 [2009-07-12 21:33 UTC] allyouneedis at gmail dot com
count counts one element to much on a freshly created array, goes to 
normal behaviour after unset()

(apache newest stable, php newest stable, fresh setup just to confirm 
the bug, windows 7)

Reproduce code:
$sockets = array(0,1,2,3,4,5);
for($i=0,$j=count($sockets);$i<$j;++$i)echo $sockets[$i];
echo '<br>';
for($i=0,$j=count($sockets)+1;$i<$j;++$i)echo $sockets[$i];
echo '<hr>';
for($i=0,$j=count($sockets);$i<$j;++$i)echo $sockets[$i];
echo '<br>';
for($i=0,$j=count($sockets)+1;$i<$j;++$i)echo $sockets[$i];

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-12 21:57 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

That's quite expected, count() returns 6, hence your "for" generates $i from 0 to 5.

When you delete the key 3, count is 5, hence for generates $i from 0 to 4, which is the reason you don't get the value 5.
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Jan 17 22:01:22 2020 UTC