PHP isset() for Checking Variables and Array Values

PHP isset() returns true when a variable exists and its value is not null. Undefined variables and null give false. An empty string, numeric 0, and the Boolean false all count as set.

Check whether a variable has a non-null value

Pass a variable or an access such as an array element to isset($value). To check a function call’s result, assign it to a variable first. isset is a PHP language construct. See the PHP manual on isset() for the rules.

After unset($value), isset($value) returns false. The check does not raise an undefined-variable warning for a variable that was never defined.

Run the example with six values

With PHP installed, save the code in UTF-8 as isset-example.php. In the same folder, run the first command to check the syntax and the second to execute it. See the PHP basics and execution guide if this is your first PHP file.

<?php
$nullValue = null;
$blank = '';
$zero = 0;
$disabled = false;
$text = 'Lorem';

var_dump(isset($missing));
var_dump(isset($nullValue));
var_dump(isset($blank));
var_dump(isset($zero));
var_dump(isset($disabled));
var_dump(isset($text));
php -l isset-example.php
php isset-example.php

var_dump() prints each result as bool(true) or bool(false). The output follows the order in the table. The example intentionally leaves $missing undefined.

bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
Variable Assigned value or state isset() result
$missing Undefined false
$nullValue null false
$blank Empty string '' true
$zero Number 0 true
$disabled Boolean false true
$text String 'Lorem' true

Check several variables at once

isset($a, $b) returns true only if both variables exist and are non-null. It checks arguments from left to right and stops at the first one that fails. Run this snippet as a separate PHP file. The empty string passes the first check, and the null value fails the second check.

<?php
$name = '';
$label = 'Lorem';
$value = null;

var_dump(isset($name, $label));
var_dump(isset($value, $name));
bool(true)
bool(false)

Distinguish a missing key from a null value

For arrays, isset($profile['nickname']) returns false for a missing key and for a key with a null value. Use array_key_exists() to tell those cases apart. Run the next snippet as a separate PHP file.

<?php
$profile = ['nickname' => null, 'attempts' => 0];

var_dump(isset($profile['nickname']));
var_dump(array_key_exists('nickname', $profile));
var_dump(isset($profile['timezone']));
var_dump(array_key_exists('timezone', $profile));
var_dump(isset($profile['attempts']));
var_dump(empty($profile['attempts']));
var_dump($profile['nickname'] ?? 'guest');
var_dump($profile['attempts'] ?? 3);
bool(false)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
string(5) "guest"
int(0)

The nickname key exists, so array_key_exists() returns true. Neither check passes for the absent timezone key. array_key_exists() searches only the first level of the array you pass to it.

Choose the check that matches your intent

If 0 is valid data, isset() can check its presence. The PHP manual on empty() treats zero as empty. If you need a nonblank string, check it against the rules for your application.

Use the null coalescing operator ?? to choose a default for a missing or null value. In the example, nickname becomes 'guest' and attempts stays 0.

A passing check does not establish a type or range. If the source data may not be an array, use is_array() before checking its keys.