Difference between if($example), if(!empty($example))?

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
meee
Forum Newbie
Posts: 18
Joined: Wed Feb 04, 2009 1:38 pm

Difference between if($example), if(!empty($example))?

Post by meee »

What makes a difference between if(!empty($example)) and if($example)?
I don't find any example when this would print a different result.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Difference between if($example), if(!empty($example))?

Post by Darhazer »

They won't print different result, but the first one is more readable, especially if you are dealing with array. If the variable is not an array, there is no difference (and again, the difference would be just in the readability).

Other than that, the only difference is that empty() won't triger notice if the variable is not defined.
meee
Forum Newbie
Posts: 18
Joined: Wed Feb 04, 2009 1:38 pm

Re: Difference between if($example), if(!empty($example))?

Post by meee »

Now I see about the trigger notice. So I assume that recommended use is isset, right?
Post Reply