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.
if (isset($_GET['page'])) { echo $_GET['page']; } var_dump(isset($a, $b)); // true only if both are set
Try it
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.
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
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
nullcounts as not set.$x = null; isset($x)isfalse. To tell "key exists with null" from "key missing", usearray_key_exists()orproperty_exists().- Falsy values are set.
0,"","0",falseand[]all givetrue. Useempty()when you want to treat them as missing too. - Nested access is safe.
isset($a['x']['y']['z'])returnsfalsewithout warnings if any level is absent ornull. It also works when an intermediate value is a string or int. - Uninitialized typed properties are treated as not set:
isset($box->unset)isfalseand 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'givesfalse. issetvs??.$a['k'] ?? 'default'uses the same test and replaces mostisset(...) ? ... : ...code; see Null coalescing operator.- Not for function results. To test a function's return value use
!== null.isset()cannot take an expression likeisset(getName()). - After
unset, a variable is no longer set.