A problem that I have run into a couple of times in getting results back from forms to the controller, and then going to another view, is that, for some reason, the conditional I set to go to a particular view did not function properly. The usual culprit is the ISSET condition.

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 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.

Let's go over some examples comparing both ISSET and EMPTY.

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

When would this mess up your conditional? Well supposed you have a $_SESSION variable, like $_SESSION[user_id] that is set when a user logs in. If you were checking to see if your user logged in, you want to make sure you use ISSET, not EMPTY, that way, if the user didn't log in, and therefore $_SESSION['user_id'] never got set, the check would be false. EMPTY would give you a true reading, and you wouldn't know that the user had not logged in to the site. Later in the code, you could end up trying to debug why your $_SESSION[user_id] wasn't set.

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 = '';
$dblQuoteVar = "";

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 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.

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.

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.

Comments are closed.