Mastering PHP’s empty() Function: When Does a Variable Count as Empty?

This article explains how PHP’s built‑in empty() function determines whether a variable is considered empty, lists the specific cases it treats as empty, provides practical code examples, and offers guidance on proper usage versus isset() for form validation and logical checks.

php Courses
php Courses
php Courses
Mastering PHP’s empty() Function: When Does a Variable Count as Empty?

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

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

If the variable’s value is 0 or the string "0".

If the variable’s value is false or null.

If the variable is an empty array ( array()) or an object with no properties.

If the variable has never been set.

Example code demonstrating empty() usage:

<?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 code outputs:

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 applications, empty() is often used to validate form input, for example:

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

Note that empty() can only be used with variables; it cannot directly test constants or expressions. To check a constant, use isset(), and for expression truthiness, use an if statement.

In summary, empty() is a handy PHP function for checking whether a variable is empty, useful for form validation and logical decisions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPform-validationemptyvariable checking
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

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.