Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP isset() language construct

Reference for PHP isset(): test whether variables, array keys and properties exist and are not null, with multiple arguments and the null pitfall.

isset() answers one question: does this variable, array element or property exist, and is its value something other than null? It returns true only when both are the case. It never raises a warning for missing things, which makes it the standard guard before reading optional input such as $_GET['page'].

Despite its function-like look, isset is a language construct. It cannot be used as a callback string, and it only accepts variables and variable-like expressions, not arbitrary values.

PHP
if (isset($_GET['page'])) {
    echo $_GET['page'];
}
var_dump(isset($a, $b));   // true only if both are set

Try it

PHP What counts as set

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Several arguments, properties and strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
isset(mixed $var, mixed ...$vars): bool
Name Description
$var A variable, array element ($a['k']), property ($o->p) or static property. Anything that is not variable-like is a compile error.
...$vars More expressions. The result is true only if every one of them is set. Evaluation stops at the first one that is not.

Return value: true if every expression exists and is not null, otherwise false.

Errors: none for missing variables, keys or properties. Passing something like isset(strlen("x")) or a constant is a fatal compile error.

Usage notes

  • null counts as not set. $x = null; isset($x) is false. To tell "key exists with null" from "key missing", use array_key_exists() or property_exists().
  • Falsy values are set. 0, "", "0", false and [] all give true. Use empty() when you want to treat them as missing too.
  • Nested access is safe. isset($a['x']['y']['z']) returns false without warnings if any level is absent or null. It also works when an intermediate value is a string or int.
  • Uninitialized typed properties are treated as not set: isset($box->unset) is false and does not throw.
  • Magic properties. On objects, isset() calls __isset() when the property is inaccessible or missing; for nested access __get() may be called too.
  • Strings. isset($s[3]) checks that offset 3 exists. A non-numeric offset such as 'x' gives false.
  • isset vs ??. $a['k'] ?? 'default' uses the same test and replaces most isset(...) ? ... : ... code; see Null coalescing operator.
  • Not for function results. To test a function's return value use !== null. isset() cannot take an expression like isset(getName()).
  • After unset, a variable is no longer set.