PHP isset() vs. empty() vs. is_null() | Envato Tuts+ (2024)

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, I'll show 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.

1
<?php
2
3
$fruit = '';
4
5
if(isset($fruit)) {
6
 echo 'Do you like '.$fruit.'?';
7
}
8
// Output: Do you like ?
9
10
if(!empty($fruit)) {
11
 echo 'Do you like '.$fruit.'?';
12
}
13
// No Output
14
15
?>

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

1
<?php
2
3
function my_empty($x) {
4
 return !isset($x) || $x == false
5
}
6
7
?>

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.

1
<?php
2
3
$fruit = NULL;
4
5
if(isset($fruit)) {
6
 echo 'Do you like '.$fruit.'?';
7
}
8
// No Output
9
10
if(is_null($fruit)) {
11
 echo 'There is no fruit.';
12
}
13
// Output: There is no fruit.
14
15
?>

PHPempty() vs. is_null()

Theempty()function returnstrueif 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 returntrueonly if the variable has the value NULL.

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

1
<?php
2
3
$person = ['first_name' => 'Monty', 'last_name' => '', 'age' => '83', 'fav_movie' => NULL];
4
5
if(empty($person['last_name'])) {
6
 if(is_null($person['last_name'])) {
7
 echo 'The last name is set to NULL.';
8
 } else {
9
 echo 'The last name is probably an empty string.';
10
 }
11
}
12
// Output: The last name is probably an empty string.
13
14
if(is_null($person['fav_movie'])) {
15
 echo $person['first_name'].' did not specify a favorite movie.';
16
}
17
// Output: Monty did not specify a favorite movie.

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.

PHP isset() vs. empty() vs. is_null() | Envato Tuts+ (2024)
Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 6120

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.