ZetCode

PHP isset Keyword

last modified November 25, 2026

The PHP isset construct checks whether a variable, array element, or property exists and is not null. It is the standard way to guard optional data and avoid warnings about undefined variables and array keys.

isset is often confused with empty and with array_key_exists. The three answer different questions, and choosing the right one prevents both warnings and logic errors.

Basic definitions

isset is a language construct, not a function, although it looks like one. It reports whether a variable, array element, or property exists and does not hold null. The syntax is isset($var) or isset($a, $b, $c).

With several arguments, isset returns true only if every argument is set. Evaluation stops at the first one that is not, so the construct is convenient for checking a group of required values.

A variable that was never declared, a variable set to null, and a variable removed with unset all give false. A value such as 0, "", or false is still set: only null and non-existence are special.

The argument must be a variable, an array or string offset, or a property. Expressions such as isset(strlen($s)) are a parse error, because isset does not evaluate a value; it inspects the symbol.

Basic usage

The first example checks a variable before using it. isset does not emit a warning when the variable is missing, which makes it safe for optional data.

basic.php
<?php

declare(strict_types=1);

$name = "John";
$nickname = null;

if (isset($name)) {
    echo "Name is set: $name\n";
} else {
    echo "Name is not set\n";
}

var_dump(isset($nickname));

The name is set, so the first branch runs. The nickname exists but contains null, so isset returns false for it. Notice that isset answers a question about existence, not about usefulness: an empty string would also be "set".

$ php basic.php
Name is set: John
bool(false)

Which values count as set

Values that are false in a boolean context are not necessarily unset. This example checks 0, an empty string, false, null, and a key removed with unset.

null_unset.php
<?php

declare(strict_types=1);

$values = [
    'zero' => 0,
    'empty string' => '',
    'false' => false,
    'null' => null,
    'pending' => 'waiting',
];

unset($values['pending']);

foreach ($values as $key => $value) {
    printf("%-14s %s\n", $key, isset($values[$key]) ? 'set' : 'not set');
}

Only null and the removed key fail the test. The distinction matters in forms and configuration: a retry count of 0 or an intentionally empty string is a real value and must not be replaced by a default merely because it is falsy.

$ php null_unset.php
zero           set
empty string   set
false          set
null           not set

Arrays and null values

For arrays, isset($array['key']) returns false when the key is missing or its value is null. array_key_exists checks only the key, so it returns true for a key whose value is null.

arrays.php
<?php

declare(strict_types=1);

$user = [
    'name' => 'John',
    'email' => null,
];

var_dump(isset($user['name']));
var_dump(isset($user['email']));
var_dump(isset($user['address']));
var_dump(array_key_exists('email', $user));

$data = ['user' => ['name' => 'Alice']];
var_dump(isset($data['user']['name']));
var_dump(isset($data['admin']['name']));

The nested check isset($data['user']['name']) is safe even when the outer key does not exist; isset does not warn about the missing intermediate key. Use array_key_exists when null is a meaningful value that must be distinguished from absence.

$ php arrays.php
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)

Checking several variables

A single call can check a list of variables. The result is the logical AND of the individual checks.

multiple.php
<?php

declare(strict_types=1);

$firstName = "John";
$lastName = "Doe";
$age = null;

var_dump(isset($firstName, $lastName));
var_dump(isset($firstName, $lastName, $age));

if (isset($firstName, $lastName, $age)) {
    echo "All variables are set\n";
} else {
    echo "Not all variables are set\n";
}

The first call returns true, the second returns false because $age is null. This is a compact way to verify that all required fields of a request or a configuration are present before processing them.

$ php multiple.php
bool(true)
bool(false)
Not all variables are set

Object properties

isset also works with object properties. From outside the class it can see public properties only; a private property is reported as not set. An uninitialized typed property is reported as not set as well.

objects.php
<?php

declare(strict_types=1);

class User
{
    public string $name;
    private string $email;
    public ?string $phone = null;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function emailIsSet(): bool
    {
        return isset($this->email);
    }
}

$user = new User("John");

var_dump(isset($user->name));
var_dump(isset($user->email));
var_dump(isset($user->phone));
var_dump(isset($user->address));
var_dump(property_exists($user, 'email'));
var_dump($user->emailIsSet());

property_exists answers a different question: the private $email exists, but isset($user->email) is false outside the class. The method emailIsSet runs inside the class and can inspect the private property. A typed property without a value is uninitialized, not null, and also fails the check.

$ php objects.php
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)

Request data

In a web application, isset is commonly used with the superglobals $_GET, $_POST, and $_SESSION. The example assigns $_GET directly so that it also runs from the command line; in a real request PHP fills the array from the query string.

request.php
<?php

declare(strict_types=1);

// In a web application these values come from the query string.
$_GET = ['page' => '2', 'q' => '', 'sort' => 'name'];

$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$query = $_GET['q'] ?? 'all';
$sort = $_GET['sort'] ?? 'relevance';
$debug = isset($_GET['debug']) ? 'on' : 'off';

echo "page:  $page\n";
echo "query: $query\n";
echo "sort:  $sort\n";
echo "debug: $debug\n";

The page parameter is present, so it is cast to an integer. The query parameter is present but empty, and the null coalescing operator keeps that empty string rather than replacing it. The missing debug parameter falls back to off. Existence checks never replace validation: after reading a parameter, check its type, range, and format.

$ php request.php
page:  2
query: 
sort:  name
debug: off

The null coalescing operator

The ?? operator is the concise form of an isset check with a fallback. It returns its left operand when that operand exists and is not null, and its right operand otherwise. It does not emit a warning for a missing array key.

null_coalescing.php
<?php

declare(strict_types=1);

$config = [
    'timeout' => 30,
    'retries' => 0,
    'host' => null,
];

$timeout = $config['timeout'] ?? 10;
$retries = $config['retries'] ?? 3;
$host = $config['host'] ?? 'localhost';
$user = $config['user'] ?? 'guest';

echo "timeout: $timeout\n";
echo "retries: $retries\n";
echo "host:    $host\n";
echo "user:    $user\n";

$config['retries'] ??= 5;
$config['attempts'] ??= 1;

echo "retries after ??=: {$config['retries']}\n";
echo "attempts after ??=: {$config['attempts']}\n";

The values 30 and 0 are kept, while the null host and the missing user are replaced. The ??= operator assigns the right-hand value only when the left-hand side is missing or null; it leaves an existing 0 untouched. For nested arrays, every level still has to exist, so check the intermediate keys or use ?? at each level.

$ php null_coalescing.php
timeout: 30
retries: 0
host:    localhost
user:    guest
retries after ??=: 0
attempts after ??=: 1

isset versus empty

isset asks whether a value exists and is not null; empty asks whether a value is falsy. The two answers differ for 0, "0", "", false, and an empty array.

isset_vs_empty.php
<?php

declare(strict_types=1);

$values = [
    '0 (int)' => 0,
    '"0"' => '0',
    '""' => '',
    'null' => null,
    'false' => false,
    '[]' => [],
    '"hello"' => 'hello',
];

printf("%-12s %-6s %-6s\n", 'value', 'isset', 'empty');
printf("%-12s %-6s %-6s\n", '-----', '-----', '-----');

foreach ($values as $label => $value) {
    printf("%-12s %-6s %-6s\n", $label,
        isset($value) ? 'true' : 'false',
        empty($value) ? 'true' : 'false');
}

// A missing variable behaves like null for isset.
unset($value);
var_dump(isset($value));
var_dump(empty($value));

Every value in the table is set except null, but only the non-empty string is considered non-empty. A missing variable gives false for both constructs. Choose the construct that matches the question: isset for "is there a value?", empty for "is the value falsy?".

$ php isset_vs_empty.php
value        isset  empty 
-----        -----  ----- 
0 (int)      true   true  
"0"          true   true  
""           true   true  
null         false  true  
false        true   true  
[]           true   true  
"hello"      true   false 
bool(false)
bool(true)

Best practices

Use isset and ?? for optional data, but do not treat their result as validation. The following guidelines prevent the most common mistakes.

Source

PHP manual: isset PHP manual: empty PHP manual: array_key_exists PHP manual: null coalescing operator

In this article we have used isset with variables, arrays, properties, and request data, and compared it with empty, array_key_exists, and ??. See also the empty keyword, unset keyword, and superglobals tutorials.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.