PHP isset vs empty

In this tutorial, we will examine the difference between the isset function and the empty function in PHP. We will be looking at the 2 functions head to head by putting PHP isset vs empty.

Both functions share similar characteristics but the subtle differences between both should be noted. For the vast majority of the time, these functions should not be used interchangeably.

PHP isset vs empty - The isset function

Simply enough, the isset function checks to see if a variable is actually set and that its value is NOT NULL

Look at this example:

<?php
 
// variable is not set at the moment
if (isset($some_variable))
	echo "It is set<br />";
else
	echo "Variable is not set or null<br />";
 
// set the variable but make the variable null
$some_variable = null;
 
if (isset($some_variable))
	echo "It is set<br />";
else
	echo "Variable is not set or null<br />";
 
// set the variable to something other than null
$some_variable = '';
 
if (isset($some_variable))
	echo "It is set<br />";
else
	echo "Variable is not set or null<br />";
 
?>

The output is:

Variable is not set or null
Variable is not set or null
It is set

As we can see, if the variable has not been defined, if it has not been assigned a value, or if it has a value of null, then the isset function will return false. As long as the variable has a value, even if that value is an empty string, it is considered to be set.

PHP isset vs empty - The empty function

The empty function on the other hand, is much broader when it comes to determining whether or not a variable is empty. A variable is still considered empty even if its value is 0 (whether the 0 is a string or integer), if it is an empty string, if it is NULL, and even if it is FALSE.

Have a look:

<?php
 
// variable is set and empty
$some_variable; 
 
if (empty($some_variable))
	echo "It is empty<br />";
else
	echo "Variable is not empty<br />";
 
 
// variable is null
$some_variable = null;
 
if (empty($some_variable))
	echo "It is empty<br />";
else
	echo "Variable is not empty<br />";
 
 
// variable is an empty string
$some_variable = '';
 
if (empty($some_variable))
	echo "It is empty<br />";
else
	echo "Variable is not empty<br />";
 
 
//variable is set to zero
$some_variable = '0';
 
if (empty($some_variable))
	echo "It is empty<br />";
else
	echo "Variable is not empty<br />";
 
 
// variable is set to integer 0
$some_variable = 0;
 
if (empty($some_variable))
	echo "It is empty<br />";
else
	echo "Variable is not empty<br />";
 
 
// variable is set to false
$some_variable = false;
 
if (empty($some_variable))
	echo "It is empty<br />";
else
	echo "Variable is not empty<br />";
 
 
// variable is set to 'a' (something else)
$some_variable = 'a';
 
if (empty($some_variable))
	echo "It is empty<br />";
else
	echo "Variable is not empty<br />";
 
?>

The output would be:

It is empty
It is empty
It is empty
It is empty
It is empty
It is empty
Variable is not empty

As we can see, the empty function is much more strict when it comes to determining the status of a variable.

The isset function is more suited to be used when processing forms to check if a form was actually submitted. This is so, because the form could be submitted but the user just didn't enter anything, so we would need to use a function which could differentiate between those scenarios. That would make the isset function more ideal.

Of course, further processing would need to be done on the data in that scenario using the empty function to see whether or not the user submitted a value when submitting the form. We look at this below.

PHP Form Processing using isset() and empty()

To demonstrate what we just saw above, an example form processing script will be used. For this example, assume that the script will be receiving a username and password (provided by a POST request) from a login form.

To process the form we would use the following PHP snippet:

if (isset($_POST['username']) && isset($_POST['password'])
{
  // both the username and password fields have been set by the form
  // this means that the form has been submitted correctly
 
  if (!(empty($_POST['username'])) && !(empty($_POST['username'])))
  {
    // the username field and the password field both contain data
    // this means that we have received a username and password
    // attempt to login the user via a database or other mechanism
  }
  elseif (empty($_POST['username']))
  {
    // dear user, you left the username field empty
  }
  else
  {
    // dear user, you left the password field empty
  }
}
else
{
  // no variables have been set. the form was not submitted
  // generate the login form or redirect to the login form
}

We first check that the form variables have been set which would indicate that the form has been submitted. It the variables have been set, attempt to process them, else display the form to the user.

In the attempt to process the variables, we first need to check if they are empty. This is because an empty form could have been submitted. In other words, the user left the fields blank and submitted the form.

If any of the fields are empty we discover which one it is and then make that known to the user if we decide to.

We hope the above tutorial on PHP isset vs empty was useful in helping you to understand the difference between the PHP isset and empty functions.

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...