How to Check Whether a Variable is Empty in PHP?
Last Updated :
16 Jul, 2024
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';
?>
Similar Reads
How to check for empty string in PHP ? In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not.here we have some common approachesTable of ContentUsing empty() functio
4 min read
Bash Scripting - How to check If variable is Set In BASH, we have the ability to check if a variable is set or not. We can use a couple of arguments in the conditional statements to check whether the variable has a value. Â In this article, we will see how to check if the variable has a set/empty value using certain options like -z, -v, and -n. Usi
10 min read
How to Check a Column is Empty or Null in MySQL? In the databases, determining whether a column is empty or null is a common task. MySQL provides various techniques to perform this check, allowing users to filter and manipulate data efficiently. This article delves into the methods for checking if a column is empty or null in MySQL, outlining the
4 min read
How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if
2 min read
How to Check if Empty Array in C? Prerequisite: Array in C The array is a type of Data-structure that can store homogeneous data-type elements. The size of the array is fixed. Syntax: int arr[N]; here, the data type of the array is an integer, the name of an array is arr, and the size of the array is N. Example: C // C Program to de
3 min read