isset() vs. empty() Functions in PHP

John Wachira Feb 15, 2024
  1. Use the isset() Function to Check if a Variable Is Set in PHP
  2. Use the empty() Function to Check if a Variable Is Empty in PHP
  3. Use isset() and empty() Functions to Create a Validation Form in PHP
  4. Differences Between isset() and empty() Functions in PHP
isset() vs. empty() Functions in PHP

This article will introduce the PHP isset() and empty() functions. We will cover how to use the functions to do the following.

  1. Check if a variable is declared and set.
  2. Check if a variable is not empty or null.
  3. Create a PHP validation form.

Let’s jump right in.

Use the isset() Function to Check if a Variable Is Set in PHP

The isset() function checks if a variable is set. The function returns true when a variable is declared and is not null.

In the example code below, we will use the isset() function to check if two variables, $x and $y, are set.

Here are the values of our variables, $x = 0 and $y = null.

Example:

<?php
$x = 0;
//Check if our variable is set and not null
if (isset($x)) {
  echo "The variable 'x' is set and not null.<br>";
}else{
  echo "The variable 'x' either null or not set.<br>";
}
$y = null;
//Check if our variable is set and not null
if (isset($y)) {
  echo "The variable 'y' is set and not null.<br>";
}else{
  echo "The variable 'y' is either null or not set.<br>";
}
?>

Output:

The variable 'x' is set and not null.
The variable 'y' is either null or not set.

As expected, $x is not null while $y is set but null. It depends on how you want to echo out the results.

You can use the function with multiple variables. However, it only returns true when all variables are set.

Use the empty() Function to Check if a Variable Is Empty in PHP

The empty() function checks if a variable is set and not empty.

Let’s look at an example code.

<?php
$x = 0;
//Check if our variable is empty
if (empty($x)) {
  echo "The variable 'x' is empty.<br>";
}else{
  echo "The variable 'x' is not empty.<br>";
}
$y = null;
//Check if our variable is empty
if (empty($y)) {
  echo "The variable 'y' is empty.<br>";
}else{
  echo "The variable 'y' is not empty.<br>";
}
?>

Output:

The variable 'x' is empty.
The variable 'y' is empty.

The empty() function considers the following as empty.

  1. 0
  2. 0,0
  3. Null
  4. False
  5. ""

Use isset() and empty() Functions to Create a Validation Form in PHP

We will create a basic HTML form in our root directory in the example codes below. We will create an index file and save it in a folder in our root directory. Our index file will give the user a warning.

Example:

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Delftstack Tutorials</title>
</head>
<body>
<h2>Validation Form </h2>
<form action="index/index.php" method="get">// The form index.php is stored in the folder index
NAME: <input type="text" name="name" />
<br/>
E-Mail: <input type="text" name="email" />
<br/>
<input type="submit" value="validate" >
</form>
</body>
</html>

Output:

Create a Validation Form Using isset and empty

Example:

<?php
if (isset($_GET['name']) AND isset($_GET['email']))
{
$name=$_GET['name'];
$email=$_GET['email'];
}
else
{
die("Acces this file from the HTML page");
}

if(empty($name))
{
die("Enter a name");
}
else
{
if (is_numeric($name))
{
die("Name Must contain Alphabet Values");
}
else
{
echo "Name: $name <br /> Email: $email";
}
}
?>

Form our form, let’s try validating the following.

  1. Joycelyn Lee, joycelynlee@delftstack.com
  2. 12567, 1234@gmail.com

Output 1:

Input Validation Form Infos

Output 2:

Output of the Validation Form

Differences Between isset() and empty() Functions in PHP

  1. The isset() function checks if a variable is set, while the empty() function checks if a variable is set and not empty.
  2. The isset() function considers 0 as a variable, while the empty() function considers 0 as empty.
Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - PHP Function