What is the difference between empty and isset in PHP
While using empty() and isset() are language constructs to determine the validity of a variable - though they're used for different purposes and have varying behavior.
In PHP we should know how to test if the variables have a particular state, so our code does what we want to do. This is where empty() and isset() two of the most common PHP language constructs come in. But they may look the same at first but both are used for different purposes and behave differently. Today we will learn about empty() vs isset() in PHP, comparing and showing their use with some examples to help you properly use them in your PHP projects.
What is isset?
- isset() is a language construct in PHP and it refers to a conditional check if a variable is set and it is not null.
- If the variable has been definitioned and it is not null, it will return true otherwise, it will return false.
Example:
Examples:
$var1 = 0;
$var2 = null;
$var3 = '';
var_dump(isset($var1)); // true, because $var1 is set and not null
var_dump(isset($var2)); // false, because $var2 is null
var_dump(isset($var3)); // true, because $var3 is set and not null
var_dump(isset($var4)); // false, because $var4 is not set
What is empty()?
- empty() is a language construct in PHP and it checks if a variable is considered "empty".
- A variable is considered empty if it does not exist or its value is equivalent to false when it is cast to a boolean.
Variables considered empty if it is:
- "" (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)
Example:
<?php
$var1 = ""; // An empty string
$var2 = "Hello"; // A non-empty string
$var3 = 0; // Zero
$var4 = array(); // An empty array
$var5 = null; // Null
$var6 = false; // Boolean false
var_dump(empty($var1)); // Output: bool(true)
var_dump(empty($var2)); // Output: bool(false)
var_dump(empty($var3)); // Output: bool(true)
var_dump(empty($var4)); // Output: bool(true)
var_dump(empty($var5)); // Output: bool(true)
var_dump(empty($var6)); // Output: bool(true)
?>
Real example of empty() according to the use case:
$var1 = "";
$var2 = "Hello";
$var3 = 0;
$var4 = array();
$var5 = null;
$var6 = false;
if (empty($var1)) {
echo "var1 is empty.\n"; // This will be printed.
}
if (empty($var2)) {
echo "var2 is empty.\n"; // This will not be printed.
}
if (empty($var3)) {
echo "var3 is empty.\n"; // This will be printed.
}
if (empty($var4)) {
echo "var4 is empty.\n"; // This will be printed.
}
if (empty($var5)) {
echo "var5 is empty.\n"; // This will be printed.
}
if (empty($var6)) {
echo "var6 is empty.\n"; // This will be printed.
}
Key Differences
Purpose:
- isset() checks if a variable is set and is not null.
- empty() checks if a variable is empty (a broader condition than isset()).
Return Value:
- isset() returns true if the variable exists and is not null.
- empty() returns true if the variable does not exist or is considered empty.
Behavior with Non-existent Variables:
- isset() returns false for non-existent variables.
- empty() returns true for non-existent variables.
Types of Values Checked:
- isset() only checks for existence and null values.
- empty() checks for a wide range of false-like values, including 0, "", false, null, and empty arrays.
Real example of isset() according to the use case:
$var1 = "Hello";
$var2 = null;
if (isset($var1)) {
echo "var1 is set.\n"; // This will be printed.
}
if (isset($var2)) {
echo "var2 is set.\n"; // This will not be printed.
}
if (isset($var3)) {
echo "var3 is set.\n"; // This will not be printed.
}