[FX.php List] if ( $var != '' ) OR if ( !empty($var) ) -- gen'l php recommendations?

Joel Shapiro jsfmp at earthlink.net
Mon Jul 24 19:13:39 MDT 2006


Thanks Chris, that's great to see.

I liked how short  ( $var != '' ) is, but I didn't even think of  
possible PHP errors when checking for unset variables.

(This makes me wonder how many other things I do might cause "low  
level errors" in PHP.  Is there a way to check for such PHP errors,  
especially when my desired results are getting returned?  And if not,  
what risks might there be for uncaught low level errors?)

Thanks vey much,
-Joel


On Jul 24, 2006, at 5:15 PM, Chris Hansen wrote:

> Joel,
>
> First off, this is a perfectly acceptable forum for PHP questions  
> (after all, we are all PHP users =)
>
> Here's my personal preference for checking a variable:
>
> if (isset($var) && strlen(trim($var)) > 0)
> {
>     # code...
> }
>
> First off, I want to be sure that the variable is even set (if not,  
> there could be an error, someone could be attempting to access  
> something the shouldn't, etc.)  Besides which, PHP will return a  
> low level error (notice, I believe) if you otherwise check a  
> variable that isn't set.
>
> The second check ensures that my variable not only contains data,  
> but that the data is not merely a space.  (Of course, if you want  
> to allow data that is just spaces, or other padding characters,  
> you'll need to modify this.)
>
> HTH
>
> --Chris Hansen
>   FileMaker 7 Certified Developer
>   Creator of FX.php
>   "The best way from FileMaker to the Web."
>   www.iViking.org
>
>
> On Jul 24, 2006, at 5:55 PM, Joel Shapiro wrote:
>
>> Hi all
>>
>> General PHP question here.  Apologies if I shouldn't post non-FX  
>> questions.
>>
>> When checking if a variable is not empty, are there reasons to  
>> choose one of these over the other?  (or are there preferable  
>> options still?)
>>
>> if ( $var != '' )
>> {
>> 	# code...
>> }
>>
>>
>> if ( !empty($var) )
>> {
>> 	# code...
>> }
>>
>> note: it seems that isset($var) will not give me what I want,  
>> since it can return true even if $var is set to empty
>>
>> TIA,
>> -Joel
>> _______________________________________________
>> FX.php_List mailing list
>> FX.php_List at mail.iviking.org
>> http://www.iviking.org/mailman/listinfo/fx.php_list
>>
>
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list



More information about the FX.php_List mailing list