🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

Using PHP isset to Easily Check the Value of Variables

Reading time 2 min
Published Aug 5, 2019
Updated Sep 30, 2019

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 isset in PHP, you can check whether a particular variable is set.
  • It returns a boolean value, True meaning 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 unset() function.

How to Use and Write isset() Correctly

PHP isset() determines whether a particular variable is set:

Example
$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 unset() function.

The syntax for making PHP check if a variable exists with isset() is simple:

Example
isset($x)

You can use PHP isset() to check one or multiple variables, thus saving time:

Example
isset($x, $y, $z)

Note: if you check more than one variable, PHP isset will only return True if all of them are set.

DataCamp
Pros
  • 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
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified 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 isset():

Example
$x = 'EarthKitten';

var_dump(isset($x));
var_dump(empty($x));

The purpose of isset() and empty() seem alike and they both return boolean values. However, as you can see in the example, they return opposite values:

  • isset() returns True for set variables.
  • empty() returns True for unset or empty ones.

Note: one more difference is that empty() can take expressions as arguments, while isset() cannot.

PHP isset: Summary

  • To check if a certain variable is set, you can use PHP isset().
  • It is not actually a function, but a language construct, similarly as echo and print.
  • If checked variable is set, it returns a boolean value of True.
  • Variables can be unset with an unset() function.