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

Steve Hannah shannah at sfu.ca
Mon Jul 24 19:29:28 MDT 2006




On 24-Jul-06, at 6:13 PM, Joel Shapiro wrote:

> 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?

Do:
error_reporting(E_ALL);

at the beginning of your script.  This will display most errors.

> And if not, what risks might there be for uncaught low level errors?)

It just makes your code less portable, in case you distribute your  
script and some people have their error reporting set to a lower level.

Best regards

Steve


----------------------------------------
Steve Hannah
Web Services Developer

Faculty of Applied Sciences
Simon Fraser University
shannah at sfu.ca
604-268-7228
Homepage: http://www.sjhannah.com
--
Need to build a database driven web application quickly?
Try Dataface: http://fas.sfu.ca/dataface


>
> 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
>
> _______________________________________________
> 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