Re: echo count(false); == 1 ?!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 27 nov. 2013, at 19:04, Daevid Vincent <daevid@xxxxxxxxxx> wrote:

> Really? 1?? I would have expected 0 or false or something other than
> positive. *sigh*
> 
> $ php -a
> php > echo count(false);
> 1
> 
> 
> :-\


Same as with sizeof() btw (which is alias). I've did run into this issue a few years ago and decided that you should check whether the argument you're passing to count() or sizeof() is an array: problem solved.

So:

$a = $count = false;
if (is_array($a)) {
	$count = count($a);
}

The "1" result is due to type conversion, well known and discussed within this same mailing list.

unreal4u-MBP:~ unreal4u$ php -a
Interactive shell

php > $a = false;
php > print_r((array)$a);
Array
(
    [0] => 
)

Greetings.



Met vriendelijke groet,
Camilo Sperberg

----------------
W: http://unreal4u.com
T: http://twitter.com/unreal4u


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php






[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux