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

Chris Hansen chris at iViking.org
Mon Jul 24 18:15:33 MDT 2006


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
>



More information about the FX.php_List mailing list