How To Check If A PHP Variable Is Empty

How to check if a php variable is empty

One of the great things about the PHP programming language is that built-in, are lots of powerful functions that a developer can use to help aid their software. One of the main things to learn as you start out your programming journey is getting familiar with the types of things that come as default in the language. All the pre-defined functions will help save you time (just like the PHP empty function), and avoid the need for you to code something from scratch, particularly when there's already an internally built function for it. Here are some reasons why you should choose to use PHP's functions instead of coding your own.

  • There's good supporting documentation for each function
  • Much better for performance
  • Heavily tested and continually bug-fixed
  • The functions will always be improving or getting replaced by better variations
  • Being an open-source language means they'll have a stronger stand on security
  • When our developers work on your code, they'll understand the logic faster, if you've used PHP's own functions

Some of the most common internal PHP functions are the variable handling functions. These will appear a lot when you browse other people's code, whether that be on GitHub or in a programming job in which you make up a team of developers. As these functions are built into PHP directly, there's no need to load external libraries to build these extensions. That's even better knowing that with each version of PHP you work on, whether that's your own managed code, or a client's, you'll know these types of functions will be available. As of PHP 8.2, there are currently 37 variable handling functions.

When working with variables in your code, have you ever needed to check if the variable you're working with is empty? The chances are that's very true. But why would an empty variable matter, or cause any problems in our code if gone unchecked? Let's find out why.

What is the empty PHP function?

The empty PHP function is a great way to determine whether a variable is empty or not. A variable that is empty is considered this if it does not exist or if the content (value) is equal to false. Let's look at an example of both of these conditions.

First, if the variable doesn't exist, but we still check it, we will get true.

# true
var_dump(empty($test));

Secondly, if the value of a variable is false, the empty function check will return true.

$test = false;

# true
var_dump(empty($test));

Also, if the variable is set, but is blank, this will result in returning true.

$test = '';

# true
var_dump(empty($test));

Checking a variable with 0 will also return true, as the test variable is an integer.

$test = 0;

# true
var_dump(empty($test));

Also '0' (string zero), is classed as empty also.

$test = '0';

# true
var_dump(empty($test));

A null variable also is classed as empty in PHP.

$test = null;

# true
var_dump(empty($test));

A completely empty array is also classified as empty in PHP. There are better functions to use when working with arrays in PHP. Especially if you're checking if keys exist and or values are in an array.

$test = [];
# false
var_dump(empty($test));

It's also good to point out that the empty function does not trigger a PHP variable undefined warning when checking a variable that does not exist. It is equivalent to the "isset" function in that regard.

Here are some examples of empty returning false, which are polar opposites to the above.

$test = 123;

# false
var_dump(empty($test));

An array, although technically empty, isn't classed as empty as it contains one element.

$test = [''];

# false
var_dump(empty($test));

The empty PHP function takes in any mixed type of variable and will always return a bool.

When to use empty instead of the isset function in PHP

The isset function in PHP is used to determine if a variable is set in the code, whereas the empty function in PHP checks if the variable contains an empty value. Both have similarities in the ways of checking PHP variables and whilst both protect against triggering a PHP warning where a variable doesn't exist, you should use them in different scenarios. Typically you would use the isset function to make sure a variable is set before attempting to use it, for example, the code below checks if the variable is set before outputting the result.

$basket = ['product' => 'Book', 'price' => 10.99];

if (isset($basket['price'])) {
 echo $basket['price'];
}

Whereas, if the price was set to nothing (free), it wouldn't output anything as it evaluates to false, therefore not passing into the if statement, as we're checking that empty is not true (i.e. it is not empty).

$test = ['product' => 'Book', 'price' => 0];

if (!empty($test['price'])) {
 echo $test['price'];
}

Conclusion

Empty is a great way to check if a variable is empty, without the risk of triggering a PHP warning for a variable that wasn't previously defined.

  • The empty function takes any kind of variable
  • The empty function will always return a boolean
  • A completely empty array "[]" returns true, but "['']" will return false

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Related Dev Guides

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all