php.net |  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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: allyouneedis at gmail dot com
New email:
PHP Version: OS:

 

 [2009-07-12 21:33 UTC] allyouneedis at gmail dot com
Description:
------------
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:
---------------
<?php
$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>';
unset($sockets[3]);
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:
----------------
01234<br>012345<hr>0124<br>01245

Actual result:
--------------
012345<br>012345<hr>0124<br>01245

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-12 21:57 UTC] colder@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

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