How to Check Whether a Variable is Empty in PHP?
Last Updated :
23 Jul, 2025
Given some values of variables, the task is to check whether the variable is empty or not in PHP. An empty variable can refer to a variable that has not been set, a variable that has been explicitly set to an empty value, or a variable that contains a value that is considered empty. In this article, we will explore different approaches to check whether a variable is empty in PHP.
Approach 1: Using empty() Function
The empty() function is a built-in PHP function that checks whether a variable is empty. A variable is considered empty if it does not exist, or if its value is equal to false, 0, "" (an empty string), "0" (a string containing zero), null, or an empty array.
PHP
<?php
$var1 = null;
$var2 = "";
$var3 = 0;
$var4 = "0";
$var5 = false;
$var6 = [];
$var7 = "Hello";
echo "Is variable 1 empty? " . (empty($var1) ? 'true' : 'false') . "\n";
echo "Is variable 2 empty? " . (empty($var2) ? 'true' : 'false') . "\n";
echo "Is variable 3 empty? " . (empty($var3) ? 'true' : 'false') . "\n";
echo "Is variable 4 empty? " . (empty($var4) ? 'true' : 'false') . "\n";
echo "Is variable 5 empty? " . (empty($var5) ? 'true' : 'false') . "\n";
echo "Is variable 6 empty? " . (empty($var6) ? 'true' : 'false') . "\n";
echo "Is variable 7 empty? " . (empty($var7) ? 'true' : 'false');
?>
OutputIs variable 1 empty? true
Is variable 2 empty? true
Is variable 3 empty? true
Is variable 4 empty? true
Is variable 5 empty? true
Is variable 6 empty? true
Is variable 7 empty? false
Explanation:
- The empty() function returns true if the variable is empty and false otherwise.
Approach 2: Using isset() Function
The isset() function is another built-in PHP function that checks whether a variable is set and is not null.
PHP
<?php
$var1 = null;
$var2 = "";
$var3 = 0;
$var4 = "0";
$var5 = false;
$var6 = [];
$var7 = "Hello";
echo "Is variable 1 empty? " . (isset($var1) && $var1 === '' ? 'true' : 'false') . "\n";
echo "Is variable 2 empty? " . (isset($var2) && $var2 === '' ? 'true' : 'false') . "\n";
echo "Is variable 3 empty? " . (isset($var3) && $var3 === 0 ? 'true' : 'false') . "\n";
echo "Is variable 4 empty? " . (isset($var4) && $var4 === '0' ? 'true' : 'false') . "\n";
echo "Is variable 5 empty? " . (isset($var5) && $var5 === false ? 'true' : 'false') . "\n";
echo "Is variable 6 empty? " . (isset($var6) && $var6 === [] ? 'true' : 'false') . "\n";
echo "Is variable 7 empty? " . (isset($var7) && $var7 === '' ? 'true' : 'false');
?>
OutputIs variable 1 empty? false
Is variable 2 empty? true
Is variable 3 empty? true
Is variable 4 empty? true
Is variable 5 empty? true
Is variable 6 empty? true
Is variable 7 empty? false
Explanation:
- The isset() function returns true if the variable is set and is not null. Also, Additional comparisons are used to check for specific empty values like an empty string, zero, or false.
Approach 3: Using strlen() Function and Custom Function
The strlen() function can be used to check if a string is empty by measuring its length. For other data types, a custom function can be created to check if the variable is empty.
Example: The strlen() function returns the length of a string. If the length is 0, the string is empty.
PHP
<?php
$var1 = null;
$var2 = "";
$var3 = 0;
$var4 = "0";
$var5 = false;
$var6 = [];
$var7 = "Hello";
echo "Is variable 1 empty? " . (empty($var1) ? 'true' : 'false') . "\n";
echo "Is variable 2 empty? " . (empty($var2) ? 'true' : 'false') . "\n";
echo "Is variable 3 empty? " . (empty($var3) ? 'true' : 'false') . "\n";
echo "Is variable 4 empty? " . (empty($var4) ? 'true' : 'false') . "\n";
echo "Is variable 5 empty? " . (empty($var5) ? 'true' : 'false') . "\n";
echo "Is variable 6 empty? " . (empty($var6) ? 'true' : 'false') . "\n";
echo "Is variable 7 empty? " . (empty($var7) ? 'true' : 'false');
?>
OutputIs variable 1 empty? true
Is variable 2 empty? true
Is variable 3 empty? true
Is variable 4 empty? true
Is variable 5 empty? true
Is variable 6 empty? true
Is variable 7 empty? false
Approach 4: Using is_null() Function
The is_null() function in PHP is used to check whether a variable is null. While it does not directly check for other empty values like empty strings or zero, it can be part of a broader strategy to determine if a variable is empty. This approach is especially useful when you need to specifically distinguish between null and other types of empty values.
Example: The following example demonstrates how to use the is_null() function to check if a variable is null, combined with additional checks for other empty values.
PHP
<?php
function isVariableEmpty($var) {
if (is_null($var) || $var === "" || $var === false || $var === 0 || $var === "0" || (is_array($var) && empty($var))) {
return true;
}
return false;
}
$var1 = null;
$var2 = "d";
$var3 = false;
$var4 = 0;
$var5 = "0";
$var6 = [];
$var7 = "Non-empty string";
echo isVariableEmpty($var1) ? 'true' : 'false';
echo "\n";
echo isVariableEmpty($var2) ? 'true' : 'false';
?>
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance