Do you know why <?= count(false) ?> returns 1?
- 
                    possible duplicate of [count of false gives 1 and if of an empty array gives false. why?](http://stackoverflow.com/questions/3776882/count-of-false-gives-1-and-if-of-an-empty-array-gives-false-why) – mario Mar 10 '13 at 21:20
4 Answers
It's specified behavior:
If var is not an array or an object with implemented Countable interface, 1 will be returned.
According to http://php.net/manual/en/function.count.php
 
    
    - 29,338
- 17
- 103
- 134
Because false is also a value and if the count() does not get array but a valid variable it returns true which is 1.
$result = count(null);
// $result == 0
$result = count(false);
// $result == 1
 
    
    - 77,474
- 47
- 185
- 261
- 
                    1"Random" comment: it's like how if(0) returns false and if(-1) returns true. – Scott Yang Mar 10 '13 at 21:17
- 
                    2You're close: it doesn't return `true`. `count` is expecting an array and if `var` isn't one, it gets cast into an array. Since `false` is a valid value, it gets cast to an array with one element: an element with the the value `false`. http://justinsomnia.org/2007/12/in-php-countfalse-returns-1/ – Nick Pickering Mar 13 '13 at 17:34
A nice way to remember this in your mind:
- count(false) is basically the same as:
- count ("one boolean"), and therefore there are "ONE" booleans as result.
 
    
    - 31
- 2
It looks to me like PHP is preventing one from using count() to determine if an element is an array or an object. They have dedicated functions for this (is_array(), is_object()) and it may be tempting to naively use count() and check for a false condition to determine array or object. Instead, PHP makes non-objects, non-arrays return 1 (which is truthy) so that this method cannot be be naively used in this way (since 0 is a valid, falsy result for an empty array/object).
This may be the why behind the choice of value to be returned by the function in the situation you're describing.
 
    
    - 444
- 4
- 6
- 
                    He's not trying to determine if it's an array or object, he's trying to determine the length of the array and questioning why a 0-record or non-existent array is still returning 1. – jerebear Sep 22 '17 at 05:10
