When you’re coding in PHP, your code holds multiple variables more often than not. It’s not that hard to get lost among them either. To make PHP check if a variable exists, you can use PHP isset(). Its name is rather self-explanatory: by using isset in PHP, you check whether a particular variable in the script is set or not.
PHP isset: Main Tips
- By using
issetin PHP, you can check whether a particular variable is set.
- It returns a boolean value,
Truemeaning a variable is set.
- Just as echo and print, isset in PHP is a language construct and not a function.
- To unset a particular variable in PHP, use the
How to Use and Write isset() Correctly
isset() determines whether a particular variable is set:
$x = 'SpaceDoggo'; var_dump(isset($x));
It returns a boolean value, which is only
True if all three conditions below are satisfied:
- The variable is declared (has a value assigned).
- This value is not Null.
- The variable hasn't been unset using the
The syntax for making PHP check if a variable exists with
isset() is simple:
You can use PHP
isset() to check one or multiple variables, thus saving time:
isset($x, $y, $z)
Note: if you check more than one variable, PHP isset will only return True if all of them are set.
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
Checking Variables in PHP: isset vs empty
To check the value of a certain PHP variable, you can also use PHP
empty(). However, you should understand how different it is to
$x = 'EarthKitten'; var_dump(isset($x)); var_dump(empty($x));
The purpose of
empty() seem alike and they both return boolean values. However, as you can see in the example, they return opposite values:
Truefor set variables.
Truefor unset or empty ones.
Note: one more difference is that empty() can take expressions as arguments, while isset() cannot.