Page 1 of 1

Empty or ' '

Posted: Tue Jul 28, 2009 8:23 am
by Addos

Code: Select all

if($row_GetAll['success'] == ''){ etc }
 
if(empty($row_GetAll['success']))  { etc }
Is there a preference as to which I should use with the above? I’m not sure if one is better or more suitable in different circumstances.

Thanks for any tips

Re: Empty or ' '

Posted: Tue Jul 28, 2009 8:30 am
by marty pain
I use empty() more frequently, as this tests for NULL, '', and 0 for numerics (check here http://uk3.php.net/manual/en/function.empty.php for more). I would think that == '' would have a performance advantage, but not sure what the recommended usage is.

Re: Empty or ' '

Posted: Tue Jul 28, 2009 8:46 am
by jackpf
I generally use == null. I only use empty() if I think the variable might be an array.

Or I use is_null() or === null if I'm checking against an actual null value.

Re: Empty or ' '

Posted: Tue Jul 28, 2009 10:33 am
by Addos
Thanks for the replies. I often get stuck trying to keep track of integers and strings and for example I was testing to see if a field in the db was 0 (zero) and wasn’t sure if I should go with:-

Code: Select all

if($row_GetAll ['success'] == ''){
if($row_GetAll ['success'] == '0'){
if(empty($row_GetAll ['success']){
I guess that as the field in the db is 0(int) then I should be testing for a 0 value.

Re: Empty or ' '

Posted: Tue Jul 28, 2009 11:35 am
by jackpf
Wow I never knew null == an empty array. How interesting :D



Oh hey, I just noticed something. You know phpBB use empty() to check if stuff is...well, empty. Since empty() thinks '0' == '', if you just try and post 0, it'll treat your post as empty. How stupid...go ahead, try it :D