Difference between isset() and empty()

Discover the differences between the isset() and empty() functions in PHP with our comprehensive guide. Learn when to use each function, and how to avoid common pitfalls when working with them. Improve your PHP programming skills today!

One of the most important aspects in PHP it’s validation. It is literally everywhere and in any kind of web app you are building or you will ever build. That is why it is crucial to know how to validate data and how to properly handle the logical conditions and guards. If you are a developer, you already know that are some conditions that even if they don’t break the code down by producing an error they do cause problems if their logic behind them is faulty.

To handle validation in PHP you absolutely need to know of those two functions (if you don’t know what a function is, read more here) isset() and empty(). Those functions are there to guard your data’s validity and ensure proper functionality across the channel’s flow of information.

Isset checks the existence of a variable and it will return true if the contents of the variable have a value and it is not null. Null, in terms of usage, means you don’t exist in PHP. It will return an empty result otherwise (false).

Keep in mind that if anything checks out to true, you get 1. For example:

$var = '';
echo isset($var); // prints: 1
$var = null;
echo isset($var); // prints nothing

As you can see, once the value of the variable becomes null, the isset returns false (nothing essentially). Now that we have the base case set up we can test for the empty function. Empty will return 1 (or true) if the contents of the variable are 0, ” (empty string) or null. Nothing (false) otherwise.

$var = '';
echo empty($var); // prints: 1
$var = 23;
echo empty($var); // prints nothing
$var = 0;
echo empty($var); // prints: 1
$var = null;
echo empty($var); // prints: 1

Usually, those two functions are used together to fulfill the validation needs of the other. You’ll see them when developing form logic for submits, populating fields of a template page (if you’re using a framework) and what have you. Understand that there are better ways to check and validate certain types of variables. You might need to look into regular expressions. Much more powerful and much more versatile. As a developer, you probably know that you can pass various validators just by entering a bunch of spaces to the corresponding input element. How would you account for that? Well, the obvious one is to check the strlen of the string variable and if that returns something greater than zero, well there you have it. It won’t solve the logical case however, because multiple space characters will also be accounted for, so you’ll get something greater that zero either way. There’s a better way to solve this issue. Regex.

$name = '        ';
if (preg_match('/^\s+$/', $name)) {
    echo 'Hold on. All spaces';
} else {
    echo 'No spaces. Good to go.';
}

Suppose that $name has been handled accordingly to the script given a certain HTTP method GET/POST and the contents are multiple spaces. That’s a problem. What we need to do is check for a pattern of multiple spaces from the start (^) of the string all the way to the end ($) and in between we need 1 or more space characters (\s). That’s fairly simple to write and you don’t need a lot of mumbo jumbo.