Discussion:
echo count(false); == 1 ?!
Daevid Vincent
2013-11-27 18:04:28 UTC
Permalink
Really? 1?? I would have expected 0 or false or something other than
positive. *sigh*

$ php -a
php > echo count(false);
1


:-\
Camilo Sperberg
2013-11-27 18:14:17 UTC
Permalink
Post by Daevid Vincent
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
Tsvetan Nikolov
2013-11-27 18:14:42 UTC
Permalink
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.
Post by Daevid Vincent
Really? 1?? I would have expected 0 or false or something other than
positive. *sigh*
$ php -a
php > echo count(false);
1
:-\
Daevid Vincent
2013-11-27 18:58:49 UTC
Permalink
-----Original Message-----
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".
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tsvetan Nikolov
2013-11-27 19:16:49 UTC
Permalink
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 ;)
Post by Daevid Vincent
-----Original Message-----
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".
Aziz Saleh
2013-11-27 18:14:44 UTC
Permalink
Post by Daevid Vincent
Really? 1?? I would have expected 0 or false or something other than
positive. *sigh*
$ php -a
php > echo count(false);
1
:-\
http://us3.php.net/count

The manual is a great place to figure out why things happen a certain way.

Aziz
Daevid Vincent
2013-11-27 18:54:49 UTC
Permalink
-----Original Message-----
Sent: Wednesday, November 27, 2013 10:15 AM
To: Daevid Vincent
Subject: Re: [PHP] echo count(false); == 1 ?!
Post by Daevid Vincent
Really? 1?? I would have expected 0 or false or something other than
positive. *sigh*
$ php -a
php > echo count(false);
1
:-\
http://us3.php.net/count
The manual is a great place to figure out why things happen a certain way.
The manual page does not explain WHY that logic is used and even inconsistent since null returns 0. It only says that it does return 1.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David OBrien
2013-11-27 18:59:04 UTC
Permalink
You're only counting ONE thing in this case a single boolean value so

count(false) == 1

with NULL you are counting zero things

count(null) == 0
Post by Daevid Vincent
-----Original Message-----
Sent: Wednesday, November 27, 2013 10:15 AM
To: Daevid Vincent
Subject: Re: [PHP] echo count(false); == 1 ?!
Post by Daevid Vincent
Really? 1?? I would have expected 0 or false or something other than
positive. *sigh*
$ php -a
php > echo count(false);
1
:-\
http://us3.php.net/count
The manual is a great place to figure out why things happen a certain
way.
The manual page does not explain WHY that logic is used and even
inconsistent since null returns 0. It only says that it does return 1.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sebastian Krebs
2013-11-27 19:49:02 UTC
Permalink
Post by Daevid Vincent
-----Original Message-----
Sent: Wednesday, November 27, 2013 10:15 AM
To: Daevid Vincent
Subject: Re: [PHP] echo count(false); == 1 ?!
Post by Daevid Vincent
Really? 1?? I would have expected 0 or false or something other than
positive. *sigh*
$ php -a
php > echo count(false);
1
:-\
http://us3.php.net/count
The manual is a great place to figure out why things happen a certain
way.
The manual page does not explain WHY that logic is used and even
inconsistent since null returns 0. It only says that it does return 1.
Actually it does, but on a different page [1], because at the end it
behaves like "count((array) $foo)"
Post by Daevid Vincent
For any of the types: integer, float, string, boolean and resource,
converting a value to an array results in an array with a single element
with
Post by Daevid Vincent
index zero and the value of the scalar which was converted. In other
words, (array)$scalarValue is exactly the same as array($scalarValue).

And some lines below
Post by Daevid Vincent
Converting NULL to an array results in an empty array.
[1] php.net/language.types.array.php#language.types.array.casting
Post by Daevid Vincent
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
github.com/KingCrunch
Tsvetan Nikolov
2013-11-27 19:51:59 UTC
Permalink
very nice explanation!
Post by Sebastian Krebs
Post by Daevid Vincent
-----Original Message-----
Sent: Wednesday, November 27, 2013 10:15 AM
To: Daevid Vincent
Subject: Re: [PHP] echo count(false); == 1 ?!
Post by Daevid Vincent
Really? 1?? I would have expected 0 or false or something other than
positive. *sigh*
$ php -a
php > echo count(false);
1
:-\
http://us3.php.net/count
The manual is a great place to figure out why things happen a certain
way.
The manual page does not explain WHY that logic is used and even
inconsistent since null returns 0. It only says that it does return 1.
Actually it does, but on a different page [1], because at the end it
behaves like "count((array) $foo)"
Post by Daevid Vincent
For any of the types: integer, float, string, boolean and resource,
converting a value to an array results in an array with a single element
with
Post by Daevid Vincent
index zero and the value of the scalar which was converted. In other
words, (array)$scalarValue is exactly the same as array($scalarValue).
And some lines below
Post by Daevid Vincent
Converting NULL to an array results in an empty array.
[1] php.net/language.types.array.php#language.types.array.casting
Post by Daevid Vincent
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
github.com/KingCrunch
Tim Behrendsen
2013-11-27 18:25:52 UTC
Permalink
Why? count() counts the number of objects in an array. Since we're
giving it a scalar value, then it's one value. Would you expect
count(array(false)) to give a zero or a false? How about count(0) versus
count(1)?

You might be confusing false and null, which are not the same thing.
False is a boolean number, null is an empty set. You'll note that
count(null) gives a zero.

This is actually one of the cases where php is doing something logical. :)

Tim
Post by Daevid Vincent
Really? 1?? I would have expected 0 or false or something other than
positive.*sigh*
$ php -a
php > echo count(false);
1
Jim Lucas
2013-11-27 19:41:12 UTC
Permalink
Post by Daevid Vincent
Really? 1?? I would have expected 0 or false or something other than
positive. *sigh*
$ php -a
php > echo count(false);
1
:-\
So, in the manual [1], it says that count expects either an array [2] or
countable object [3] as the first param. If you pass it something other then
those two types, why would expect it to behave as if you had passed it an
array? I would think one should expect unexpected results if giving
unexpected input.

1 http://php.net/count#refsect1-function.count-description
2 http://us3.php.net/manual/en/language.types.array.php
3 http://us3.php.net/manual/en/class.countable.php
--
Jim Lucas

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