PHP isset() Function

PHP isset() checks if a variable is set and its value is not NULL. It returns true or false.

It is one of the most used functions in PHP, specially when you work with forms, $_GET, $_POST and arrays. Before using a value, you can check it with isset() to avoid “undefined variable” or “undefined index” warnings.


Syntax

isset(mixed $var, mixed ...$vars): bool

Parameters

ParameterDescription
varRequired. The variable you want to check
varsOptional. More variables to check. isset() returns true only when all of them are set

Return value: true if the variable exists and is not NULL, otherwise false.

Note: isset() is not a normal function, it is a language construct. So you cannot pass a function result in it, like isset(getName()). PHP will give an error.

Examples

Example #1: Check a variable

Here the variable $name has a value, but $city was never created.

<?php
$name = "Robin";

var_dump(isset($name));
var_dump(isset($city));
?>

Output:

Tutorials Class - Output Window

bool(true)
bool(false)


Example #2: NULL, 0 and empty string

A variable with NULL value is treated as not set. But 0 and empty string are real values, so isset() returns true for them. Many beginners get confuse here.

<?php
$a = null;
$b = 0;
$c = "";

var_dump(isset($a));
var_dump(isset($b));
var_dump(isset($c));
?>

Output:

Tutorials Class - Output Window

bool(false)
bool(true)
bool(true)


Example #3: Check many variables at once

You can pass more than one variable. The result is true only when all variables are set.

<?php
$name = "Robin";
$age = 25;

var_dump(isset($name, $age));
var_dump(isset($name, $city));
?>

Output:

Tutorials Class - Output Window

bool(true)
bool(false)


Example #4: Check array keys

isset() works with array keys too. In this example the “email” key exists, but its value is NULL, so the result is false.

<?php
$user = ["name" => "Robin", "email" => null];

var_dump(isset($user["name"]));
var_dump(isset($user["email"]));
var_dump(isset($user["phone"]));
?>

Output:

Tutorials Class - Output Window

bool(true)
bool(false)
bool(false)


Example #5: Default value using ?? operator

The null coalescing operator (??) is a shortcut of isset() with a default value. It is available from PHP 7.0.

<?php
$user = ["name" => "Robin"];

// Same as: isset($user["city"]) ? $user["city"] : "Not given"
$city = $user["city"] ?? "Not given";

echo $city;
?>

Output:

Tutorials Class - Output Window

Not given


isset() vs array_key_exists()

isset() returns false when the value is NULL. If you want to know that a key exists even with a NULL value, use array_key_exists().

<?php
$user = ["email" => null];

var_dump(isset($user["email"]));
var_dump(array_key_exists("email", $user));
?>

Output:

Tutorials Class - Output Window

bool(false)
bool(true)


Related functions

  • empty() checks if a variable is empty (0, empty string, NULL and more).
  • is_null() checks only for the NULL value.

Summary

  • isset() returns true when a variable exists and is not NULL.
  • NULL means “not set” for isset(), but 0 and empty string are set.
  • You can check many variables in one call.
  • Use ?? when you need a default value.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.