Difference between ISSET and EMPTY
So many differences and little similarities between ISSET and EMPTY in php.
ISSET
Isset returns TRUE if the variable exists and has a value other than NULL.
<?php
$message = null;
if(isset($message){
return "message is set";
}
return "message is not set";
//returns "message is not set";
$message = "";
if(isset($message){
return "message is set";
}
return "message is not set";
//returns "message is set";
$array = ["foo" => "bar", "bar" => "foo"];
if(isset($array["food"]){
return "food is set";
}
return "food is not set";
//returns "food is not set";
EMPTY
Empty checks to see if a variable is empty. such as: "" (an empty string), 0 (integer), 0.0 (float)`, "0" (string), null, false
$message = null;
if(!empty($message){
return "message is not empty";
}
return "message is empty";
//returns "message is empty";
$message = "";
if(empty($message){
return "message is empty";
}
return "message is not empty";
//returns "message is empty";
$array = ["foo" => "bar", "bar" => "foo"];
if(empty($array["food"]){
return "food is empty";
}
return "food is not empty";
//returns "food is empty";
You can see the difference between Isset and Empty... if a variable is set to any value apart from null, Isset returns true including if the variable is set to false, but Empty covers everything including empty string and false