|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76470 count(false)===1
Submitted: 2018-06-13 13:53 UTC Modified: 2018-06-13 14:16 UTC
From: mordyg at gmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 7.1.18 OS: N/A
Private report: No CVE-ID: None
 [2018-06-13 13:53 UTC] mordyg at gmail dot com

[] is falsey, so count(false) should equal count([])

Code shows otherwise.

Test script:
echo []?'Y':'N'; // outputs N
echo false?'Y':'N'; //outputs N
echo count([])?'Y':'N'; //outputs N
echo count(false)?'Y':'N'; //outputs Y


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-06-13 13:59 UTC] mordyg at gmail dot com
Just to clarify ..
count(null) ===0

So if you say this is not a bug as it a non-countable, then you have a new bug, that count(null) should be 1, not 0
 [2018-06-13 14:01 UTC]
-Status: Open +Status: Not a bug
 [2018-06-13 14:01 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at
> Returns the number of elements in array_or_countable. When the parameter is neither an array nor an object with
> implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0
> will be returned.
 [2018-06-13 14:04 UTC]
This is likely to not be fixed as although it's clearly not great behaviour, it is how it has been for ages, so fixing it would 'break' lots of 'working' code.

However from PHP 7.2, trying to count a non-countable thing gives a warning notice:

If you have an error handler, that converts all warnings to exceptions, the behaviour is then acceptable.
 [2018-06-13 14:05 UTC]
See also for why it works like it does. But as @danack mentioned, this kind of stuff is exactly why count() generates a warning starting with PHP 7.2.
 [2018-06-13 14:05 UTC] mordyg at gmail dot com
You have 2 exceptions ... why not make it 3?
 [2018-06-13 14:16 UTC]
This is not another exception, as an undefined variable access returns NULL. I've removed the note in, as it is both redundant and nowadays quite unnecessary.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 08:02:42 2024 UTC