A very common mistake when first getting in to PHP is to think that isset() and empty() can be used as each others inverse. This is VERY far from the truth and can cause major problems in an application. In this quick article I will explore the differences between isset and empty in PHP
isset
isset will determine if a variable is set and is not NULL. This simply means if the variable has NEVER been set to anything or it is null isset will return false otherwise it will return true. Obviously you can add an ! before it to reverse this depending on it's purpose in your application. Here is a quick example
var_dump(isset($var)); //will output false since it is not set $var=null; var_dump(isset($var)); //will output false since it is null $var=0; var_dump(isset($var)); //will output true $var=""; var_dump(isset($var)); //will output true $var="0"; var_dump(isset($var)); //will output true $var=array(); var_dump(isset($var)); //will output true $var=false; var_dump(isset($var)); //will output true $var="this is a string"; var_dump(isset($var)); //will output true
empty
empty is quite a bit different than isset. First of all, the most obvious thing is that it works in the opposite way of isset, but there is much more to it than that. Empty will return true for everything isset would return false for PLUS it will also return true for an empty string, an empty array, the string "0″, the number 0, or false. Notice the differences in the same example as above using empty instead of isset.
var_dump(empty($var)); //will output true since it is not set $var=null; var_dump(empty($var)); //will output true since it is null $var=0; var_dump(empty($var)); //will output true 0 is considered empty $var=""; var_dump(empty($var)); //will output true "" is an empty string $var="0"; var_dump(empty($var)); //will output true "0" is empty $var=array(); var_dump(empty($var)); //will output true for an empty array $var=false; var_dump(empty($var)); //will output true since bool false is considered empty $var="this is a string"; var_dump(empty($var)); //will output false $var=2; var_dump(empty($var)); //will output false since 2 is a valid number
As you can see they both have very different results and can clearly NOT be used interchangeably. For a REALLY detailed chart of how different variables test out check out this variable test chart. Enjoy.
baba
June 9, 2011 at 7:04 am
your note-book style site is awesome
Alon
February 1, 2012 at 8:02 am
Thank you !! now its clear for me
Mike
July 24, 2012 at 4:32 pm
What would you suggest to use to check if a submit button has been pressed? I’ve been using if(isset($_POST['login_submit'])) and was wondering if there’s any downside to that which I haven’t thought of yet.
Lenny
October 8, 2012 at 9:44 pm
I would personally use if(!empty($_POST['login_submit'])) … is set will probably work fine, but if for some weird reason a blank field is passed through isset wouldn’t catch it. It’s doubtful that it’d be an issue, but it’s certainly not impossible ESPECIALLY if someone is trying to mess with your form (hacker, etc). !empty .. will check if it’s set AND that it’s not blank (“” , false, or 0)
Kamaludeen
March 15, 2013 at 2:18 am
Yours explanation style is very easy to understand. Thank you so much.
pradeep
July 10, 2013 at 11:41 am
very clear with the concept its really helpful to me