1. Use empty() insted of isset() ;
Isset() checks if a variable has a value including ( Flase , 0 , or Empty string) , But not NULL.
In other words, a variable is set if it has been assigned a value other than NULL.
On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False.
Returns FALSE if var has a non-empty and non-zero value.
In other words, a variable is empty if it is an empty string, 0, “0″, false, NULL, array(), and an unset variable are all empty.
<?php
$var = NULL;
if(isset($var)){
echo " I am set". PHP_EOL;
}
if(empty($var)){
echo " I am empty". PHP_EOL;
}
?>
Output :
I am empty
<?php
$var = '';
if(isset($var)){
echo " I am set". PHP_EOL;
}
if(empty($var)){
echo " I am empty". PHP_EOL;
}
?>
Output :
I am set
I am empty
Note: Now when you are validating forms to make sure a user did not leave a form field blank, it is probably best to use neither empty() or isset(). Since it is possible your form might accept 0 as a valid answer. Therefore you should just check to make sure it is not an empty string.
<?php
if($_GET['var'] == "") {
echo "You must enter a value for var!". PHP_EOL;
}
?>