Table of contents
1.
Introduction
2.
Definition and Usage 
3.
Syntax
4.
Parameter Values
5.
Return Value
6.
Technical Details
7.
Frequently Asked Questions
7.1.
What is the difference between empty() and isset()?
7.2.
Can empty() be used on object properties?
7.3.
Does empty() generate a warning if the variable doesn't exist?
8.
Conclusion 
Last Updated: Oct 30, 2024
Easy

PHP isempty() Function

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Just like any other programming language, PHP also helps programmers with many useful built-in functions that make it easier to check & validate data. One such function is empty(), which helps determine if a variable is empty or not. This function is very simple to use but is a powerful tool that is very useful for writing robust PHP code. 

PHP isempty() Function

In this article, we'll learn what the empty() function does, how to use it with code examples, & some important things to remember about this function.

Definition and Usage 

The empty() function in PHP checks if a variable is empty. It returns true if the variable is an empty string, 0, 0.0, "0", null, false, or an empty array. Otherwise, it returns false.

Using empty() is a convenient way to check if a variable has a meaningful value before using it in your code. This helps prevent errors and unexpected behavior that can occur when working with undefined or empty variables.

Some common use cases for empty() are:

1. Validating user input from forms
 

2. Checking if an array has any elements
 

3. Ensuring a variable is set before accessing it
 

4. Handling default values for function arguments

Syntax

The syntax for the empty() function in PHP is :

empty(mixed $var): bool


It takes a single argument `$var`, which is the variable you want to check. The function returns a boolean value - either true or false.

For example : 

<?php
$x = '';
if (empty($x)) {
   echo 'The variable $x is empty.';
} else {
   echo 'The variable $x is not empty.';
}
?>
You can also try this code with Online PHP Compiler
Run Code


Output

The variable $x is empty.


In this case, the empty() function will return true because `$x` is an empty string. The script will output: "The variable $x is empty."

You can also use empty() directly in an if statement's condition:

<?php
$y = 0;
if (empty($y)) {
   echo 'The variable $y is considered empty.';
}
?>
You can also try this code with Online PHP Compiler
Run Code


Output

The variable $y is considered empty.


Since 0 is considered empty by the empty() function, this script will output: "The variable $y is considered empty."

Note: Remember that empty() doesn't generate any warning or error if the variable doesn't exist. This makes it a useful tool for checking variables without causing unintended side effects.

Parameter Values

The empty() function accepts a single parameter `$var`, which can be any expression that evaluates to a variable. This includes simple variables, arrays, object properties, or function results.

Let’s see some examples of valid arguments for empty():


1. Simple variable:

   $x = '';
   empty($x);


2. Array element:

   $arr = ['a', 'b', 'c'];
   empty($arr[1]);


3. Object property:

   $obj = new stdClass();
   $obj->prop = 0;
   empty($obj->prop);


4. Function result:

   function returnEmptyString() {
       return '';
   }
   empty(returnEmptyString());


The empty() function will evaluate the given argument & determine if it is empty according to the following rules:

- "" (an empty string)

- 0 (integer)

- 0.0 (float)

- "0" (string)

- NULL

- FALSE

- array() (an empty array)


Any variable that fits one of these conditions will be considered empty.

It's important to note that empty() only checks the value of the variable itself. It does not recursively check nested arrays or object properties. For example:

$arr = [[]];
empty($arr);    // Returns false, as $arr is an array with one element
empty($arr[0]); // Returns true, as $arr[0] is an empty array

Return Value

The empty() function returns a boolean value indicating whether the provided argument is considered empty or not.

It will return true if the variable is:

- An empty string ""

- An integer 0

- A float 0.0

- A string "0"

- NULL

- FALSE

- An empty array array()
 

In all other cases, empty() will return false.

These are a few examples showing the return values of empty():

<?php

var_dump(empty(""));       
var_dump(empty(0));         
var_dump(empty(0.0));       
var_dump(empty("0"));       
var_dump(empty(NULL));      
var_dump(empty(FALSE));     
var_dump(empty(array()));   

var_dump(empty("abc"));     
var_dump(empty(1));         
var_dump(empty(1.5));       
var_dump(empty(TRUE));      
var_dump(empty(array(1)));  

?>
You can also try this code with Online PHP Compiler
Run Code


Output

bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)

 

These return values allow you to easily use the empty() function in conditional statements to control the flow of your script based on whether a variable is considered empty or not.

For example:

$x = '';
if (empty($x)) {
    echo 'The variable $x is empty.';
} else {
    echo 'The variable $x is not empty.';
}
You can also try this code with Online PHP Compiler
Run Code


This script will output: 

"The variable $x is empty."

Technical Details

Generally, the empty() function is a language construct rather than a regular function. This means it has some special properties & behaves differently than user-defined functions.

When you call empty(), PHP evaluates the argument expression & checks the resulting value against a set of conditions. The exact implementation may vary between PHP versions, but the general behavior remains consistent.

In PHP 8.0 & later, empty() follows these rules to determine if a value is considered empty:
 

1. If the value is an empty array, it returns true.
 

2. If the value is a string & its length is 0, it returns true.
 

3. If the value is NULL, it returns true.
 

4. If the value is a numeric string "0", it returns true.
 

5. If the value is a number 0 (integer or float), it returns true.
 

6. If the value is FALSE, it returns true.
 

7. If the value is a SimpleXML object created from empty tags, it returns true.
 

8. In all other cases, it returns false.


One important thing to note is that empty() doesn't generate any warnings or errors if the variable doesn't exist. This is different from isset(), which only returns true if a variable is set & not NULL.

Let’s see an example explaining the difference:

$x = null;
var_dump(isset($x)); 
var_dump(empty($x)); // bool(true)
unset($y);
var_dump(isset($y)); // bool(false)
var_dump(empty($y)); // bool(true), no warning generated
You can also try this code with Online Javascript Compiler
Run Code


Output

bool(false)
bool(true)
bool(false)
bool(true)

 

While empty() is generally reliable & efficient, there are a few points to keep in mind:

1. empty() only checks the value of the variable itself, not nested elements.

2. The behavior of empty() can be affected by magic methods like __isset() or __empty() in objects.

3. In older PHP versions (before 5.5), empty() would also return true for the string "false".


Note: Despite these minor disadvantages, empty() remains a powerful and commonly used tool in PHP for checking whether variables are empty or false.

Frequently Asked Questions

What is the difference between empty() and isset()?

While both functions check variables, isset() only verifies if a variable exists & is not NULL, whereas empty() also considers falsey values like 0, "", or false as empty.

Can empty() be used on object properties?

Yes, empty() can check object properties. However, the behavior may be affected by magic methods like __isset() or __empty() if defined in the object's class.

Does empty() generate a warning if the variable doesn't exist?

No, unlike isset(), empty() does not generate any warning or error if the variable being checked is not defined. It simply returns true in such cases.

Conclusion 

In this article, we've explained the empty() function in PHP, which is very useful for checking if variables are empty or hold false values. We discussed its definition, usage, syntax, parameter values, and return values. We also understood some technical details. 

You can also check out our other blogs on Code360.

Live masterclass