Empty or ' '

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Empty or ' '

Post 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
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: Empty or ' '

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Empty or ' '

Post 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.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Empty or ' '

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Empty or ' '

Post 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
Post Reply