isset() vs. empty() Functions in PHP
-
Use the
isset()
Function to Check if a Variable Is Set in PHP -
Use the
empty()
Function to Check if a Variable Is Empty in PHP -
Use
isset()
andempty()
Functions to Create a Validation Form in PHP -
Differences Between
isset()
andempty()
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.
- Check if a variable is declared and set.
- Check if a variable is not empty or
null
. - 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.
0
0,0
Null
False
""
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:
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.
Joycelyn Lee
,joycelynlee@delftstack.com
12567
,1234@gmail.com
Output 1:
Output 2:
Differences Between isset()
and empty()
Functions in PHP
- The
isset()
function checks if a variable is set, while theempty()
function checks if a variable is set and not empty. - The
isset()
function considers0
as a variable, while theempty()
function considers0
as empty.
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