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
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: mike24 at bellsouth dot net
New email:
PHP Version: OS:

 

 [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: Fri Nov 22 05:01:29 2024 UTC