Re: echo count(false); == 1 ?!
| From: | Camilo Sperberg | Date: | Wed, 27 Nov 2013 18:14:17 +0000 |
| Subject: | Re: echo count(false); == 1 ?! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322515@lists.php.net to get a copy of this message | ||
On 27 nov. 2013, at 19:04, Daevid Vincent <daevid@daevid.com> 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