Re: A php bug or?..
| From: | Simon J Welsh | Date: | Mon, 08 Aug 2011 20:25:53 +0000 |
| Subject: | Re: A php bug or?.. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314431@lists.php.net to get a copy of this message | ||
On 9/08/2011, at 8:20 AM, Andre Polykanine wrote:
> Hi everyone,
>
> As we all know, count() returns 1 if the variable is not an array.
> Question is: why in the world does it this? If a variable is *notA* an array, it contains
> *zero* array elements.
> You can answer: "but no, man, you can say
> $x="world";
> $y=$x{3}; // $y="l"
>
> so the variable is treated or can be treated as an array".
> Well. If strings are treated like arrays, why count($x) doesn't return 5 instead of 1?
> Just asking.
>
> --
> With best regards from Ukraine,
> Andre
I'm assuming it has to do with the value, if not an array or object, being cast as an array.
Thus, non-false equivalent values get cast into an array of size 1:
<?php
var_dump((array)1);
var_dump((array)null);
// Output
array(1) {
[0]=>
int(1)
}
array(0) {
}
---
Simon Welsh
Admin of http://simon.geek.nz/