Backend Development 5 min read

Understanding PHP's empty() Function: Usage, Behavior, and Practical Examples

This article explains PHP's built‑in empty() function, detailing the conditions under which it returns true, providing clear usage guidelines, and offering multiple code examples for variable checks, form validation, and common pitfalls.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding PHP's empty() Function: Usage, Behavior, and Practical Examples

In PHP programming, checking whether a variable is empty is a frequent requirement, and the built‑in empty() function is designed for this purpose. This article introduces the usage of empty() and provides practical code examples.

The empty() function is straightforward: it accepts a single argument and returns a boolean. It returns true when the argument meets any of the following conditions, otherwise it returns false :

1. The variable's value is 0 or the string "0" .

2. The variable's value is false or null .

3. The variable is an empty array ( array() ) or an object with no properties.

4. The variable has never been set.

Below is a sample script demonstrating the use of empty() :

<?php
$var1 = '';
$var2 = 0;
$var3 = false;
$var4 = null;
$var5 = array();
$var6; // not set

echo 'var1 is empty: ' . (empty($var1) ? 'true' : 'false') . '<br>';
echo 'var2 is empty: ' . (empty($var2) ? 'true' : 'false') . '<br>';
echo 'var3 is empty: ' . (empty($var3) ? 'true' : 'false') . '<br>';
echo 'var4 is empty: ' . (empty($var4) ? 'true' : 'false') . '<br>';
echo 'var5 is empty: ' . (empty($var5) ? 'true' : 'false') . '<br>';
echo 'var6 is empty: ' . (empty($var6) ? 'true' : 'false') . '<br>';
?>

Running the above code produces the following output:

var1 is empty: true
var2 is empty: true
var3 is empty: true
var4 is empty: true
var5 is empty: true
var6 is empty: true

In real‑world applications, developers often use empty() to verify whether user‑submitted form data is present, ensuring data validity before further processing.

<?php
if (empty($_POST['username'])) {
    echo 'Please enter a username';
} else {
    // other logic
}
?>

The snippet above checks if the submitted username field is empty; if so, it prompts the user, otherwise it proceeds with other logic.

It is important to note that empty() can only be applied to variables. To test constants or expressions, use isset() for existence checks and standard if statements for evaluating truthiness.

In summary, the empty() function is a highly useful tool in PHP for quickly determining whether a variable holds an "empty" value, and it is commonly employed for form validation, conditional logic, and other backend development tasks.

backendPHPform validationemptyvariable-check
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.