NYCPHP Meetup

[nycphp-talk] ${$variable}

Chris Snyder csnyder at chxo.com
Wed Aug 27 17:59:41 EDT 2003


Thanks Dan, I wasn't checking whether it was set or not.
And it seems like empty() is just misleading all around.

So what we really want, to test for an empty string -- I'm going to drop 
this $$foo business now, it's misleading -- is the following:

// check whether $foo is an empty string...
if ( isset($foo) ) {
    if ( is_string($foo) && $foo=='' ) do stuff here
    }

Wow. I updated http://chxo.com/scripts/emptytest.php to reflect this.



Analysis & Solutions wrote:

>Hi:
>
>On Wed, Aug 27, 2003 at 04:40:30PM -0400, Chris Snyder wrote:
>  
>
>>To test for an empty string, you have to test that it is both empty AND 
>>a string.
>>
>>if ( is_string($$foo) && empty($$foo) ) do empty string stuff here
>>    
>>
>
>Do that the other way around.  Always do an empty() check first, just in
>case the variable isn't set at all.  This prevents PHP from generating an
>"undefined variable" Notice error.
>
>Do be aware that...
>
>   $bar = 0;    // is_string() will be false, empty() will be true
>   $bar = '0';  // is_string() will be true,  empty() will be true
>
>$var == '' can be the appropriate test in some circumstances.
>
>--Dan
>
>  
>




More information about the talk mailing list