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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 13:01:29 2024 UTC