Re: echo count(false); == 1 ?!
| From: | Tsvetan Nikolov | Date: | Wed, 27 Nov 2013 19:16:49 +0000 |
| Subject: | Re: echo count(false); == 1 ?! | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322523@lists.php.net to get a copy of this message | ||
I accept your argument as logical but look at the documentation:
count() accepts 2 arguments. The first should be: "An array or countable
object". From here I can tell you that 90% of your cases do not comply with
the documentation so any argument pro or against us just speculation.
Comply with the documentation and then file bugs ;) null is neither an
array nor countable object. false also falls in that group! Use is_null for
null values or compare with true/false for bool. If you don't know the
possible range of your data the problem is not php ;)
On Wed, Nov 27, 2013 at 7:58 PM, Daevid Vincent <daevid@daevid.com> wrote:
>
>
> > -----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".
>
>
>
>