php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #1348 count() can't identify a non-array variable correctly
Submitted: 1999-04-24 16:56 UTC Modified: 1999-04-24 17:01 UTC
From: mike24 at bellsouth dot net Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.7 OS: Linux 2.0.36
Private report: No CVE-ID: None
 [1999-04-24 16:56 UTC] mike24 at bellsouth dot net
The manual says that count "returns 1 if the variable is not an array." This can produce inaccurate results if the variable is an array, but has only one element.

Temporary Solution:
Don't use count(). Instead use is_array(), isset(), and sizeof().

Solution:
Set integer returned by count() to (-1) when the tested variable is not an array.



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-04-24 17:01 UTC] rasmus
I don't see how 1 is inaccurate.  Think of a variable as a 1-element array.  count() was never designed to distinguish between arrays and variables.  It was simply designed to tell you how many elements something contains.  Having it return -1 would be counter-intuitive.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 22:01:28 2024 UTC