Re: isset
| From: | Jochem Maas | Date: | Mon, 16 Apr 2007 23:10:44 +0000 |
| Subject: | Re: isset | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-253075@lists.php.net to get a copy of this message | ||
Richard Lynch wrote:
> On Mon, April 16, 2007 5:35 pm, Tim Earl wrote:
>> What about in the following context?
>>
>> $arr = array();
>> If (!empty($arr)) { }
>>
>> This is where i have found it to be the most usefull...
>
> If I'm already certain that it's an array, I just use 'count' personally.
>
> 'empty' takes a mixed data type, and could return TRUE for, say, 'foo'
> even though you are expecting an array there.
>
> I think it's a better practice to use the "right" weapon and use
> count() on an array to see if it's empty or not.
php -r 'var_dump(count("foo"));'
outputs:
int(1)
so my strategy is usually
if (!empty($r) && is_array($r)) {
// good to go.
}
if I know it's an array I'll definitely use empty() over count() ....
count() needs to actually count the items where as empty() can return false
as soon as it finds a singel element ... maybe I'm mistaken - if so please
put me right.