- Variable is set if it has been assigned a value other than NULL.
- Even if a variable is assigned an empty string (""), it is set.
- Variable is empty if it is an empty string (""), 0, "0",
false, NULL, array(), and a variable declared but not given a value are all empty.
empty() function: returns true for the following values:
empty string (""), 0, "0", NULL, or FALSE
isset() function: returns false when the variable is NULL, that
is,
not initialized or given a value.
//both return false using isset() function
//$myvar;
//$myvar = NULL;
// Example1:
//returns true using empty() function
$myvar=0;
// Evaluates to true because $myvar contains 0
if (empty($myvar))
{
echo '$myvar is either empty string, 0, "0", empty, NULL, or FALSE';
}
else
{
echo '$myvar contains a value';
}
// Example2:
//returns false using empty() function
$myvar=5;
// Evaluates to false because $myvar contains 5
if (empty($myvar))
{
echo '$myvar is either 0, "0", empty, NULL, or FALSE';
}
else
{
echo '$myvar contains a value';
}
// Example3:
// Evaluates as true because $myvar is initialized
if (isset($myvar))
{
echo '$myvar is initialized';
}
else
{
echo '$myvar does not contain a value';
}
// Example4:
//unset() destroys specified variables
unset($myvar);
// var_dump() returns NULL
var_dump($myvar);
// Example5:
// Evaluates as false because $myvar is destroyed
if (isset($myvar))
{
echo '$myvar is initialized';
}
else
{
echo '$myvar does not contain a value';
}
Example1:$myvar is either 0, "0", empty, NULL, or FALSE
Example2:$myvar contains a value
Example3:$myvar is initialized
Example4:
Warning: Undefined variable $myvar in
D:\InetPub\vhosts\mjowett-1638.package\qcitr.com\wwwroot\demos\isset_vs_empty.php on line
152
NULL
Example5:$myvar does not contain a value