PHP empty() function use with MySQL NULL

PHP provides a handy function, empty(), that is used to determine whether a variable is empty. Perhaps that is a bit confusing to someone unfamiliar with the empty() function and I can see how. In this blog post, I will cover: what empty() means in PHP, what the empty() function does, and a use case pairing up empty() with the PHP ternary operator conditional construct. Both used in combination with the MySQL NULL value. Continue reading and see examples of empty()


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


I’ll use these various variables for examples throughout the post:
$name = 'Josh';
$another_name = NULL;
$some_name = '';
$an_age = 0;
$another_age = '0';
$different_age = 12;

Before we see what values are returned when the above variables are passed to empty(), let’s visit the PHP documentation on empty() for additional information, that way we know what to expect.

Here is the description:

“Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.”

empty() returns a value – either TRUE or FALSE – depending on if the variable that is passed to empty() as an argument exists and is either non-empty or non-zero.

So what exactly is non-empty and non-zero? Let’s find out…

Echo PHP empty() function in the browser

For more context, I’ll echo out the return values of each of the below calls to empty(), passing in the previously defined variables above as arguments in each call:

echo '$name variable empty value is: '.empty($name).'<br />';
echo '$another_name variable empty value is: '.empty($another_name).'<br />';
echo '$some_name variable empty value is: '.empty($some_name).'<br />';
echo '$an_age variable empty value is: '.empty($an_age).'<br />';
echo '$another_age variable empty value is: '.empty($another_age).'<br />';
echo '$different_age variable empty value is: '.empty($different_age).'<br />';

php code echoed out in the browser
Return results from empty echo’ed in the browser

Understanding PHP empty() function

What does empty consider to be empty? empty() considers any of these values to be empty:
  • The empty string – “”
  • The integer 0 (zero)
  • The float 0.0
  • The string “0”
  • The NULL value.
  • The FALSE boolean
  • An empty array().

Based on the above list, the first variable, ‘$name’, and the last variable ‘$different_age’, are not considered empty, as they both have actual values (none of which are those included on the empty list). When passed to empty() as arguments, both variables return FALSE whereas all of the other variables return TRUE.

In PHP, a FALSE can be considered 0 (zero) where a TRUE value can be considered 1. For more information on PHP booleans, visit the online PHP Booleans documentation.

In order to see the actual boolean result, we can use the var_dump() function, passing the empty() function call with the variable parameter as var_dump()‘s own parameter:

echo var_dump(empty($name)).'<br />';
echo var_dump(empty($another_name)).'<br />';
echo var_dump(empty($some_name)).'<br />';
echo var_dump(empty($an_age)).'<br />';
echo var_dump(empty($another_age)).'<br />';
echo var_dump(empty($different_age)).'<br />';

php code to echo out boolean values
Using var_dump to echo out boolean values.

What can we use the PHP empty() function for?

Now that we have an idea of what empty() does, we can use it in a simple example and see the benefits of such a handy function.

I have recently used an empty() trick in a LAMP stack application I have been working on and just had to share it here on my blog (although it is by no stretch of the imagination, sacred knowledge).

Suppose we have this MySQL table, structure, and data:

phpMyAdmin table data
Current data in the project_pdf table

The requirement is, provide a simple button for each project name in the table. However, each rows’ button should only be clickable if there is a pdf document file for that projects’ table row. We can use empty() to test the ‘pdf_doc’ column variable and determine if the value is NULL or not and set the button accordingly. Recall NULL is considered empty and if passed to the empty() function will return TRUE.

We first need a connection to the database along with a SELECT query, retrieving all rows from the ‘project_pdf’ table:

include __DIR__.'/includes/DatabaseConnection.php';
$query = "SELECT `id`, `project_name`, `pdf_doc` FROM `project_pdf` ORDER BY `project_name` ASC;";
$results = $pdo->query($query);
foreach ($results as $row) {
    $rows[] = ['id' => $row['id'], 'project_name' => $row['project_name'], 'pdf_doc' => $row['pdf_doc']];
}

The next part of the script contains the dynamic PHP, which provides a list of Bootstrap buttons; one for each of the returned records from the MySQL database:

php and html code in foreach loop
PHP foreach loop using empty() function to set button attributes

Notice the call to empty() in the button class attribute. Using the PHP ternary operator, each button’s ‘$row[‘pdf_doc’]’ value is checked.

The ternary operator uses this syntax:

    conditional_test ? truth_result_code : false_result_code

If the ‘$row[‘pdf_doc’]’ value is NULL, then empty() returns TRUE. That buttons’ class attribute is set to btn btn-secondary disabled along with the disabled attribute itself.

Should the ‘$row[‘pdf_doc’]’ value not be empty() (returns false), then the class attribute is set to btn btn-primary (with no disabled attribute) and stays active.

The following screenshots show the active and inactive buttons for each MySQL table row:

html list of Bootstrap buttons
Button list of all current projects

mouse pointer on Bootstrap button in active state
Button in active state.

mouse pointer on Bootstrap button inactive state
Mouse cursor on Bootstrap inactive button.

Notice the ‘Project 3’ button is inactive since its row in the database has a NULL value for the ‘pdf_doc’ column.

Be sure and check out these 2 posts on storing and retrieving a .pdf file with PHP and the MySQL BLOB datatype:

I hope through this post, you can find ways to use empty() for similar types of functionality where you see fit. I think it’s pretty neat myself. What creative ways have you used empty()? Tell me about them in the comments below. I’d love to know and learn more uses myself.

Like always, if you see anything in the code I can improve on or correct, please let me know via the comments below.

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.



To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the β€˜Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.