Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why count(false) return 1?

Tags:

php

count

Do you know why <?= count(false) ?> returns 1?

like image 756
Jeremie Ges Avatar asked Mar 10 '13 20:03

Jeremie Ges


2 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

like image 117
Chris Laplante Avatar answered Oct 09 '22 00:10

Chris Laplante


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
like image 36
Starx Avatar answered Oct 09 '22 00:10

Starx