RE: [PHP] echo count(false); == 1 ?!
| From: | Daevid Vincent | Date: | Wed, 27 Nov 2013 18:58:49 +0000 |
| Subject: | RE: [PHP] echo count(false); == 1 ?! | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322521@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Tsvetan Nikolov [mailto:live.webscore@gmail.com]
> Sent: Wednesday, November 27, 2013 10:15 AM
> To: Daevid Vincent
> Cc: PHP-General
> Subject: Re: [PHP] echo count(false); == 1 ?!
>
> Just think about it. When was the last time you counted something and the
> result was false? It makes no sense. Logically counting should return
> negative, 0 or positive value.
Well in my case I have a method that populates a property. The property
starts out as null (since it was never loaded). If there is an error, the
method returns false, otherwise it fills the array.
We could argue about flow/logic/etc. and how to "fix" my code.
But logically, given how null/false/0 are usually treated, almost
interchangeably, such as
$foo = false;
$foo = 0;
$foo = null;
If (!foo) ....
All do the same thing
It would stand to reason that count() would return 0 for anything that isn't
an array, as there are ZERO elements in the "array".