PHP isset() – Check if A Variable is Set, With Examples

Using PHP isset()

This article shows you how to use the PHP isset() function to check whether a variable exists and is populated.

So, it’s checking if a variable is set.

Why would you want to do this?

You can’t perform actions on a variable that doesn’t exist or that has no value – you’ll either get unexpected output or an error.

So, being able to check that a variable is set and is populated is pretty useful.

Syntax for isset()

isset ( $var1 , $var2...) 

Note that:

  • $var1 should be a variable of any type
    • It can also be an array element or object attribute if you wish to check whether a value is set in an existing variable – see examples below
    • Passing a value directly to isset() will result in an error – it must be a variable
  • You can provide more variables, separated by commas
  • At least one variable should be passed to isset()
  • isset() returns a boolean value
    • FALSE if it is not set or has a value of NULL
    • TRUE if the variable is set

Example use of isset()

Here is some simple example usage of the isset() function:

$var = 1;
$var2 = NULL;

if (isset($var) == TRUE) {
    # The variable $var is set and is not NULL, so isset() returned TRUE

if (isset($var2) == FALSE) {
    # The variable $var2 is set but has a NULL value, so isset() returned FALSE

if (isset($var3) == FALSE) {
    # The variable $var3 is not set, so isset() returned FALSE

Example – Checking if an Element Exists in an Array

isset() can also be used to check whether a value is set for an array key or position in an array. It can also check for deeper array values for multidimensional arrays:

$fruit_colours = array('oranges' => 'orange', 'lemons' => NULL, 'apples' => array('granny_smith' => 'green', 'macintosh' => 'red'));

var_dump(isset($fruit_colours['oranges'])); // TRUE as the array key 'oranges' exists in the $fruit_colours array and is not NULL
var_dump(isset($fruit_colours['limes'])); // FALSE as the array key 'limes' does not exist inf the $fruit_colours array
var_dump(isset($fruit_colours['hello'])); // FALSE as though the  array key 'lemons' is set, it has a value of NULL

var_dump(isset($fruit_colours['apples']['macintosh'])); // TRUE as $fruit_colours contains a value at the array key 'apples', and the array stored at 'apples' has an array key with value 'macintosh' which is not NULL
var_dump(isset($fruit_colours['apples']['gala'])); // FALSE as though $fruit_colours contains a value at the array key 'apples', the array stored at 'apples' doesn't contain a value at array key 'gala'

I’m using the PHP var_dump() function to print out information on the return value from isset() – find out more here.

Example – Checking if an Object has an Attribute/Property

Just as isset() can be used to check if an array key is set, it can be used to check whether an object property is set:

$car = (object) array('make' => 'ford', 'model' => 'fairlane', 'details' => array('colour' => 'blue', 'year' => NULL));

var_dump(isset($car->make)); // TRUE as the $car object has a 'make' property which is not NULL
var_dump(isset($car->wheels)); // FALSE as the $car object does not have a 'wheels' property

var-dump(isset($car->details->colour)); // TRUE as the $car object has a 'details' property with a 'colour' subproperty, which has a value which is not NULL
var-dump(isset($car->details->year)); // FAlSE as while the $car object has a 'details' property, the 'year' subproperty is NULL

In the latter two examples, you can see that you can check the sub-properties of an object without first checking the parent properties – if any parent property is missing at any depth, isset() will return FALSE.


Want more examples? Check out the official documentation.


Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *