W3docs

isset()

The isset() function is a built-in function in PHP that checks whether a variable has been set and is not null. It returns true if the variable exists and is

Introduction

The isset() construct is a built-in language construct in PHP that checks whether a variable has been set. It returns true if the variable exists and is not null, and false otherwise.

The key thing to understand is that isset() cares only about existence, not truthiness. It returns true even when the value is 0, false, "0", or an empty string "" — it returns false only for variables that were never defined, that have been unset, or that hold null.

This makes isset() the safe way to test array keys and form fields before you read them, avoiding the "Undefined variable" and "Undefined array key" warnings PHP emits when you access something that does not exist.

This page covers the syntax of isset(), how it behaves with null, 0, and missing array keys, how to check several variables at once, and how it differs from the related empty(), is_null(), and array_key_exists() functions.

Syntax

The syntax of the isset() construct is as follows:

The PHP syntax of isset()

bool isset(mixed $var [, mixed $... ])

The construct takes one or more parameters, $var and optional additional parameters separated by commas. Each parameter represents a variable to be checked for being set. The construct returns true if all the variables exist and are not null, and false otherwise.

Example Usage

Here is an example of how to use the isset() construct in PHP:

Example of PHP isset()

<?php
$var1 = "hello";
$var2 = null;
$var3 = 0;
$array = ['key' => 'value'];

if (isset($var1)) {
    echo '$var1 is set and is not null' . "\n";
}
if (isset($var2)) {
    echo '$var2 is set and is not null' . "\n";
} else {
    echo '$var2 is not set or is null' . "\n";
}
if (isset($var3)) {
    echo '$var3 is set (value is 0)' . "\n";
}
if (isset($array['key'])) {
    echo "Array key 'key' is set" . "\n";
}
?>

In this example, we define several variables and an array. We use the isset() construct to check whether each variable or array key is set. The first if statement returns true because $var1 is set. The second if statement returns false because $var2 is null, so the else block executes. The third if statement returns true because $var3 is set to 0 (demonstrating that isset() checks for existence, not truthiness). The fourth if statement returns true because the array key 'key' exists.

Checking Several Variables at Once

isset() accepts multiple arguments and returns true only when every argument is set and non-null. As soon as one of them is missing or null, the whole call returns false. This is handy for validating that a group of required values is all present before continuing:

<?php
$name  = "Ada";
$email = "[email protected]";
$phone = null;

if (isset($name, $email)) {
    echo "Both name and email are set" . "\n";
}

if (isset($name, $email, $phone)) {
    echo "All three are set" . "\n";
} else {
    echo "At least one of name, email, phone is missing or null" . "\n";
}
?>

Because $phone is null, the second isset() returns false even though the other two are set.

Nested Arrays and Missing Keys

A common reason to reach for isset() is reading deeply nested data, such as $_POST or a decoded JSON structure, where any level might be absent. isset() safely short-circuits: if an intermediate key does not exist, it returns false instead of raising a warning.

<?php
$user = [
    'profile' => [
        'name' => 'Grace',
    ],
];

var_dump(isset($user['profile']['name']));   // bool(true)
var_dump(isset($user['profile']['age']));    // bool(false) - key missing
var_dump(isset($user['settings']['theme'])); // bool(false) - 'settings' missing entirely
?>

Note that isset() returns false for an existing key whose value is null. If you must distinguish "key exists but holds null" from "key does not exist," use array_key_exists() instead.

isset() vs empty(), is_null() and array_key_exists()

These four are easy to confuse. The table shows what each returns for a variable $x holding different values:

Value of $xisset($x)empty($x)is_null($x)
"text"truefalsefalse
0 / 0.0 / "0"truetruefalse
"" (empty string)truetruefalse
nullfalsetruetrue
undefined / unsetfalsetruewarning + true

Key takeaways:

  • Use isset() when you want to know whether something exists and is not null — it never raises a warning on undefined variables.
  • Use empty() when you want to know whether a value is "falsy" (0, "", null, false, empty array, etc.).
  • Use is_null() when you specifically care about the null value — but note it warns if the variable was never defined.
  • Use array_key_exists() when a key may legitimately hold null and you still need to detect its presence.

The Null Coalescing Shortcut

Since PHP 7, the null coalescing operator ?? is a concise alternative to an isset() check followed by a fallback. The expression $a ?? $b evaluates to $a if it is set and non-null, otherwise to $b — without any warning:

<?php
$config = ['timeout' => 30];

// Verbose: explicit isset()
$retries = isset($config['retries']) ? $config['retries'] : 5;
echo $retries . "\n"; // 5

// Concise: same result with the ?? operator
$timeout = $config['timeout'] ?? 60;
echo $timeout . "\n"; // 30
?>

Reach for ?? whenever you just need a default value; keep isset() when you need the boolean result itself, for example inside a larger condition.

Conclusion

The isset() construct is a useful tool for checking whether a variable has been set in PHP. It can be used to ensure that a variable exists before performing operations on it, or to handle set and unset variables in a particular way. By using this construct, developers can ensure that their code is working with the expected data and avoid errors that may occur when working with null values.

Practice

Practice
What is the function of the isset() in PHP?
What is the function of the isset() in PHP?
Was this page helpful?