Difference between ISSET and EMPTY function

Every developer have confusion in there mind while they are using ISSET() and EMPTY() .He/She always confuse where he have to use ISSET function or where they have to use !EMPTY function in php. But as of now no one have confusion in there mind because i am going to describe the difference between these two very useful function of php.

ISSET() :  ISSET checks the variable to see if it has been set, in other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a " ", 0, "0", or FALSE are set, and therefore are TRUE for ISSET.

EMPTY() : EMPTY checks to see if a variable is empty. Empty is interpreted as: " " (an empty string), 0 (0 as an integer), 0.0 (0 as a float), "0" (0 as a string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.

Value of variable ($var) isset($var) empty($var)
“” (an empty string) bool(true) bool(true)
” ” (space) bool(true)
FALSE bool(true) bool(true)
TRUE bool(true)
array() (an empty array) bool(true) bool(true)
NULL bool(true)
“0? (0 as a string) bool(true) bool(true)
0 (0 as an integer) bool(true) bool(true)
0.0 (0 as a float) bool(true) bool(true)
var $var; (a variable declared, but without a value) bool(true)
NULL byte (“\ 0?) bool(true)

Now have a look below example for better explanation of each scenario :-

A Non Declared Variable :
If I try to use a variable I haven't declared in a conditional statement to check to see if it was set, the check for both ISSET and EMPTY would look like this:

if ( isset($aNonDeclaredVariable)) -- This would be FALSE
if ( empty($aNonDeclaredVariable)) -- This is TRUE

A Variable Set to NULL :

$nullVar = NULL;

if ( isset($nullVar)) -- This would be FALSE
if ( empty($nullVar)) -- This is TRUE

This example would most likely come up if you have default NULL values set up in your database. You get the value back from your database, and check it in a conditional. Here again you want to use ISSET. EMPTY would come up true for your NULL value which mostly likely will lead to an incorrect result in your conditional.

An Empty String :

$singleQuoteVar =  '' ;    //Single quote empty variable.
$dblQuoteVar =  "" ;     // Double quote empty variable.

if ( isset($singlequoteVar))      --   This would be TRUE
if ( empty($singlequoteVar))   --   This is TRUE

if ( isset($dblquoteVar))     --   This would be TRUE
if ( empty($dblquoteVar))  --   This is TRUE

Where can you run into trouble here? They're both the same. The problem usually comes up when your using a form. For example, in a textarea input the results back would be "" if nothing was entered, or a user put a space in the form. The same with an input box. What you normally want to know is, is the field blank. It might be better here to check for an empty string directly, something like:

if ( $_POST['foo'] == "" )

A Zero Value :

$zeroValue = 0 ;

if ( isset($zeroValue))     --   This would be TRUE
if ( empty($zeroValue))  --   This is TRUE

The problem here, is we have both set a specific value, 0, in our variable, and according to the definition of EMPTY, it is empty, thus TRUE. What we normally check for when a zero value may be expected is "not empty," like so:

if ( ! empty($zeroValue))  -- This is FALSE

In order for "not empty" to be true, there needs to be some value other than
"" (an empty string), 0 (0 as an integer), 0.0 (0 as a float), "0" (0 as a string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class, as we defined above.

"Not EMPTY" is a good check any time you want to check if there is an actual value in the variable. This is a good check when you assigned a value statically, that could be changed dynamically during the course of running the program.

A Declared Array :

$arrVar = array() ;

if ( isset($arrVar))     --   This would be TRUE
if ( empty($arrVar))  --   This is TRUE

By now you should be getting the hang of this.

A Boolean Value :

$TrueBoolValue  = TRUE  == 1;
$FalseBoolValue = FALSE == 0;

if ( isset($TrueBoolValue))      --   This would be TRUE
if ( empty($TrueBoolValue))   --   This is FALSE

if ( isset($FalseBoolValue))     --   This would be TRUE
if ( empty($FalseBoolValue))  --   This is TRUE

Both variables have been set to a boolean value, which translates to a 1 in the case of TRUE. ISSET is set to 1 and thus TRUE, EMPTY is not empty thus it is FALSE.

In the case of FALSE, which translates in PHP to 0. Remember for ISSET "0" is TRUE, but for EMPTY "0" is empty, and thus EMPTY "0" is TRUE for being empty.

An Object :

class TestObj
{
public function teststr()
{
$str = "The object under test" ;
return $str ;
}}
$test     = new TestObj ;
$teststr = $test->testrun() ;

if ( isset($test))       --   This would be TRUE
if ( empty($test))    --   This is FALSE
if ( ! empty($test))  --   This is TRUE

if ( isset($test2))      --  This would be TRUE
if ( empty($test2))   --  This is FALSE
if ( ! empty($test2)) --  This is TRUE

The first test is just an object with no properties. This says we have set the variable, and it is not empty. A change in PHP in 5.0 made objects with no properties no longer considered empty. It's a blob full of object structure and data so it's not empty. The second test is a variable set by returning a result from an object method. Again the variable is set and is not empty.

One other gotcha, you should be aware of, is that both ISSET and EMPTY only check a variable. You can't put a statement together inside the parenthesis, something like ISSET(trim($stringVar)). It will throw an exception. Use ISSET and EMPTY only with a single variable like ISSET($stringVar). Do all your algorithms before doing your check.


If you like this please don't forget to leave a comment

Happy Coding :)

Post a Comment

Previous Post Next Post