isset
Checks that a variable exists and is not null.
Syntax
isset(mixed $var, mixed ...$vars): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $var | mixed | A variable, an array element or a property. |
| ...$vars | mixed | Other variables; all of them must exist. |
Return value
bool`true` if the value exists and is not `null`.
Examples
An existing and a missing variable:
<?php
$name = 'Anna';
var_dump(isset($name));
var_dump(isset($age));Output:
bool(true)
bool(false)An array element, and the short form with ??:
<?php
$user = ['name' => 'Anna'];
var_dump(isset($user['email']));
echo $user['email'] ?? 'not set';Output:
bool(false)
not setNotes
For 0, an empty string and false the function returns true — those values exist. It gives false only for null and missing variables.
See also
- empty — Checks that a variable does not exist or holds an "empty" value.
- array_key_exists — Checks whether an array has a key.
- is_null — Checks whether a value is null.
Related recipes
In the tutorial
- Variables — How to declare variables in PHP, name them and interpolate them into strings.