php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46322 count(FALSE) returns 1
Submitted: 2008-10-16 22:27 UTC Modified: 2008-10-17 03:19 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: dmitryshw at hotmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.2.6 OS: Montavista
Private report: No CVE-ID: None
 [2008-10-16 22:27 UTC] dmitryshw at hotmail dot com
Description:
------------
As title states, count for FALSE return 1. I came across the problem when creating an array through unserialize and depending on count to tell me how many elements the array has to know if I should use it. This caused a bug when the string was empty, and unserialize faithfully returned FALSE. However the result was used by the subsequent code as an array because count returned 1. According to the docs, the parameter var must have a Countable interface... so FALSE has a Countable interface??

Reproduce code:
---------------
$test = unserialize(NULL);
print count($test);

Expected result:
----------------
0

Actual result:
--------------
1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-16 22:31 UTC] dmitryshw at hotmail dot com
Should have mentioned that I saw that this is mentioned as an "example" but I don't see this being any less of a bug... unless you can please explain how count(FALSE) makes sense to return 1.
 [2008-10-16 22:50 UTC] scottmac@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

From the documentation on count.

If var  is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var  is NULL, 0 will be returned.

You probably want to look at empty() to see if it has any elements rather than use count().
 [2008-10-17 03:15 UTC] dmitryshw at hotmail dot com
I didn't say var is null, I said var is false. From the documentation you pasted, the only case when count should return anything other than 0 is if the var is an array or implements the Countable interface. How does false apply to either one of them?
 [2008-10-17 03:19 UTC] dmitryshw at hotmail dot com
Sorry I apologize, I misread the documentation (even after you pasted them here... *sigh*) that if it's NOT an array 1 is returned... I guess I'll try the support channel but I still don't understand how false (or other non-array) can return a count of 1... since that is the same result as an array length 1. To me the logical result for false would be the same as null.

Again sorry to waste your time.
 [2008-10-17 03:19 UTC] scottmac@php.net
You misread what I said.

If var is NOT an array or an object with implemented Countable
interface, 1 will be returned.

false is not an array or an object so 1 will be returned.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 14:01:35 2024 UTC