isset
The "isset" keyword is used in PHP to determine whether a variable is set and is not null. In this article, we will explore the syntax and usage of the "isset"
The PHP isset() Function
isset() is a PHP language construct that returns true when a variable both exists (has been assigned) and is not null. It returns false if the variable was never set, was explicitly set to null, or was removed with unset().
Because reading an undefined variable triggers a Warning: Undefined variable notice in PHP, isset() is the standard, side-effect-free way to ask "do I have a usable value here?" before you actually use it — for example, guarding access to $_GET, $_POST, or array keys that may not be present.
This chapter covers the syntax, the multi-argument form, how it behaves with arrays and object properties, and how it compares with related tools like empty() and the null coalescing operator.
Syntax
isset(mixed $var, mixed ...$vars): boolA minimal guard looks like this:
if (isset($myVariable)) {
echo "The variable is set.";
} else {
echo "The variable is not set.";
}isset() is a language construct, not a function call, so you do not need to include or import anything to use it, and you can pass several variables at once (covered below).
Basic examples
<?php
// Example 1 — an assigned, non-null variable
$myVariable = "Hello, world!";
echo isset($myVariable) ? "set" : "not set"; // set
echo PHP_EOL;
// Example 2 — a variable that was never declared
echo isset($someOtherVariable) ? "set" : "not set"; // not set
echo PHP_EOL;
// Example 3 — a variable explicitly set to null
$empty = null;
echo isset($empty) ? "set" : "not set"; // not setOutput:
set
not set
not setThe third case is the key gotcha: assigning null is enough to make isset() return false, even though the variable technically exists. If you need to distinguish "never declared" from "declared but null", use is_null() or array_key_exists() instead.
Checking multiple variables at once
When you pass more than one argument, isset() returns true only if every argument is set and non-null. This is handy for validating that a group of required form fields all arrived together.
<?php
$name = "Ada";
$email = "[email protected]";
$phone = null;
var_dump(isset($name, $email)); // bool(true)
var_dump(isset($name, $email, $phone)); // bool(false) — $phone is nullisset() with arrays
isset() works on array elements and safely checks nested keys without raising a warning when an intermediate key is missing.
<?php
$user = [
'name' => 'Ada',
'address' => ['city' => 'London'],
];
var_dump(isset($user['name'])); // bool(true)
var_dump(isset($user['missing'])); // bool(false)
var_dump(isset($user['address']['city'])); // bool(true)
var_dump(isset($user['address']['zip'])); // bool(false) — no warningNote that isset($array['key']) returns false when the value at that key is null. If a key may legitimately hold null and you only care whether the key is present, reach for array_key_exists().
isset() vs. empty() vs. the null coalescing operator
These three are often confused. The table shows what each returns for the same input:
Value of $x | isset($x) | empty($x) |
|---|---|---|
"hello" | true | false |
0 or "0" or "" | true | true |
null | false | true |
| undefined | false | true |
So use isset() to ask "does a real value exist here?", and empty() to ask "is this missing or falsy (empty string, 0, false, empty array)?".
Since PHP 7, the null coalescing operator ?? builds on isset() semantics to provide a default in one expression — it is the idiomatic replacement for the isset() ? : ternary:
<?php
$config = ['theme' => 'dark'];
// Verbose, pre-PHP 7 style:
$theme = isset($config['theme']) ? $config['theme'] : 'light';
// Equivalent with the null coalescing operator:
$theme = $config['theme'] ?? 'light';
echo $theme; // dark$config['missing'] ?? 'light' evaluates to 'light' with no warning, exactly because ?? checks isset() under the hood.
When to use isset()
- Guarding optional request data:
if (isset($_GET['page'])) { ... }. - Confirming required fields before processing a form (
isset($a, $b, $c)). - Safely reading possibly-missing array keys in configuration or JSON-decoded data.
- Lazily initializing a value only once:
$cache ??= computeExpensiveValue();.
Reach for a different tool when you need to treat null as "present" (array_key_exists()), check object/array contents for falsiness (empty()), or remove a variable entirely (unset()).