Quick Tip: How To Check if a Variable Is Set in PHP

Share this article

How To Check if a Variable Is Set in PHP

In this PHP quick tip, we’ll cover how to check whether or not variables are set, and if so, what their values are — which is a very common task in programming.

When checking if a variable is set in PHP, our first impulse might be to use the isset() function. And while this will work for most cases, if we look at the definition of the isset() function, we’ll see the following:

isset: determines if a variable is declared and is other than null.

However, in PHP we can set a variable to be NULL:

<?php
    $variable = NULL;
    var_dump(isset($variable));

This attribution will return false — even though we’ve explicitly set our variable to NULL! This is a condition within PHP that might generate some confusion. As we proceed below, we’ll look at the isset() method and when to use it, and what other methods we can use when isset doesn’t work.

Enter empty() and is_null()

While the isset() method can help us determine if a variable has been set with some value, it won’t help us if the variable has been set with NULL. To deal with this situation, we need to use either the empty() or the is_null() function. The empty() function will determine whether a variable is empty. It will work (that is, return true) for:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

This means we can use empty() to determine if a variable is empty or not. And in this case, both NULL and false are considered empty:

<?php
    $variable = NULL;
    var_dump(empty($variable));

We can also use the is_null() function. This function will determine whether a variable is null or not.

Here’s an example of using is_null() to determine whether a variable is or isn’t null:

<?php
    $variable = NULL;
    var_dump(is_null($variable));

The most important thing when dealing with variables in PHP is to understand the differences between isset(), is_null(), and empty(). These three functions can be used to determine the status of a variable, but they have different behaviors. The following table can be used to highlight the differences between the functions.

“” “foo” NULL FALSE 0 undefined
empty() True False True True True True
is_null() False False True False False True (error)
isset() True True False True True False

Is There a Single Function We Can Use?

Using get_defined_vars() will return an associative array with keys as variable names and values as the variable values. We still can’t use isset(get_defined_vars()['variable']) here, because the key could exist and the value is still null, so we have to use array_key_exists('variable', get_defined_vars()).

For example:

<?php
    $variable = null;
    $isset = array_key_exists('variable', get_defined_vars());
    var_dump($isset); // true

    $isset = array_key_exists('otherVariable', get_defined_vars());
    var_dump($isset); // false

By using get_defined_vars() in this way, we can be 100% sure that we’re checking if the variable is set or not.

Conclusion

In this short article, we discussed how we can check whether a variable is set in PHP or not. We also looked at the differences between set, empty and null, and the key considerations to keep in mind when using the common functions isset(), is_null(), and empty(). So, the next time you need to check the status of a PHP variable, you’ll have all the information you need so you can choose the right method and eliminate all ambiguities from your code.

Frequently Asked Questions (FAQs) about PHP Variable Checking

What is the purpose of the isset() function in PHP?

The isset() function in PHP is used to determine if a variable is set and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false. It is a useful function to prevent errors when you are trying to access a variable that may not exist or may have a NULL value.

How does the isset() function differ from the empty() function in PHP?

While both isset() and empty() functions are used to check the value of a variable, they work in slightly different ways. The isset() function checks if a variable is set and is not NULL. On the other hand, the empty() function checks whether a variable is empty. A variable is considered empty if it does not exist or if its value equals FALSE. So, empty() will return true if the variable is not set or if its value is 0, 0.0, “”, “0”, NULL, FALSE, array(), or an unset variable.

Can I use the isset() function to check if an array key exists?

Yes, you can use the isset() function to check if a specific key exists within an array. If the key exists and its value is not NULL, isset() will return true. If the key does not exist or its value is NULL, isset() will return false.

How can I check if multiple variables are set in PHP?

You can check if multiple variables are set by passing more than one variable to the isset() function. The function will return true only if all of the specified variables are set and are not NULL.

What happens if I use isset() on a variable that has been unset?

If you use isset() on a variable that has been unset, it will return false. This is because the unset() function in PHP is used to destroy a specified variable, so after a variable has been unset, it is no longer set and its value is considered to be NULL.

Can I use isset() to check if a variable is set to 0?

Yes, you can use isset() to check if a variable is set to 0. The isset() function only checks if a variable is set and is not NULL. It does not check the value of the variable. So, if a variable is set to 0, isset() will return true.

How can I use isset() to avoid undefined variable errors?

You can use isset() to avoid undefined variable errors by checking if a variable is set before trying to use it. If isset() returns false, you know that the variable is not set and you can avoid using it and thus prevent an undefined variable error.

Can I use isset() with object properties in PHP?

Yes, you can use isset() with object properties in PHP. The isset() function can be used to check if a property exists in an object. If the property exists and its value is not NULL, isset() will return true.

What is the difference between isset() and is_null() in PHP?

The isset() function checks if a variable is set and is not NULL, while the is_null() function checks if a variable is NULL. So, if a variable has been set to NULL, isset() will return false and is_null() will return true.

Can I use isset() to check if a form field has been filled in?

Yes, you can use isset() to check if a form field has been filled in. When a form is submitted, the form fields are sent to the server as variables. You can use isset() to check if these variables are set, which would indicate that the form fields have been filled in.

Claudio RibeiroClaudio Ribeiro
View Author

Cláudio Ribeiro is a software developer, traveler, and writer from Lisbon. He's the author of the book An IDE Called Vim. When he is not developing some cool feature at Kununu he is probably backpacking somewhere in the world or messing with some obscure framework.

PHP Quick Tipsvariables
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week