Friday, June 11, 2021

PHP isset() vs empty() vs is_null()

PHP isset() vs empty() vs is_null()

You will use variables in almost every program that you write using PHP. Most of the time these variables have a value, and we usually create them with an initial value. However, there is always a possibility that some of the variables you are using are not initialized. This can result in a warning from PHP about using an undefined variable.

There can be many reasons for undefined variables. The most common ones are that you either actually did not define the variable or you made a spelling mistake when reusing it somewhere else. This is just a programming bug. However, another possibility that can result in an undefined variable is that it has been defined conditionally.

You also might find that a variable has the value NULL. This also can happen for a number of reasons. For example, the variable might just have not been initialized with a value. Or, the null value might be returned from a function to signal some sort of error.

In any case, using a variable before it has been defined, or when it has a null value can have unintended consequences. In this tutorial, we will tell you how to check if an element has been defined and see if it is empty or null.

You can use isset(), empty() or is_null() to check if either one or all of those conditions are true or false.

Definitions

Let's get started with some definitions.

  1. isset(): You can use isset() to determine if a variable is declared and is different than null.
  2. empty(): It is used to determine if the variable exists and the variable's value does not evaluate to false.
  3. is_null(): This function is used to check if a variable is null.

PHP isset() vs empty()

As we saw from the definitions, isset() will return true if we have defined the variable before and set its value to something other than NULL. This can include 0, an empty string or false. On the other hand, empty() will return true whenever the variable value is set to something that evaluates to false—we call these "falsey" values. Examples of falsey values include 0, the empty string "" and the string "0", an empty array, NULL, or of course the boolean false.

One similarity between isset() and empty() is that they are both language constructs and therefore cannot be called using variable functions.

The following code snippet should explain the difference between these two.

Note that empty() can be written using the isset() function:

Of course, it's usually just easier to use the built-in empty() function.

PHP isset() vs is_null()

The is_null() function returns true if the value of a variable has been explicitly set to NULL. Otherwise, it simply returns false. On the other hand, isset() will return true as long as a variable is defined and its value is not NULL.

Here is a basic example to show the difference between them.

PHP empty() vs is_null()

The empty() function returns true if the value of a variable evaluates to false. This could mean the empty string, NULL, the integer 0, or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL.

Here is a basic example to show the difference between them.

Important Points to Remember

There are two tips that you can use to write more concise code and avoid errors in future.

1. Unlike empty() and is_null(), you can pass multiple values to isset() at once to simultaneously check if any of them is undefined or set to NULL. In that case, isset() will only return true if none of the passed values are NULL.

2. Don't use == to check if a value is NULL. This will give a false positive for falsey values like an empty string that evaluate to false.

Final Thoughts

This tutorial quickly explained the difference between isset(), empty() and is_null(). Hopefully, you will now be able to determine which of them is right for you to use in your code.


No comments:

Post a Comment