Easy Tutorial
❮ Func Array Uintersect Func Array Natcasesort ❯

PHP isset() Function

PHP Available Functions

The isset() function is used to check if a variable is set and is not NULL.

If a variable has been unset with unset() and then checked with isset(), it will return FALSE.

If a variable is set to NULL and checked with isset(), it will return FALSE.

It is important to note that the null character ("\0") is not equivalent to the PHP NULL constant.

PHP Version Requirements: PHP 4, PHP 5, PHP 7

Syntax

bool isset ( mixed $var [, mixed $... ] )

Parameter Description:

If multiple parameters are passed, isset() will return TRUE only if all parameters are set. The evaluation proceeds from left to right and stops as soon as an unset variable is encountered.

Return Value

Returns TRUE if the specified variable exists and is not NULL, otherwise returns FALSE.

Example

<?php
$var = '';

// The result is TRUE, so the text will be printed.
if (isset($var)) {
    echo "Variable is set." . PHP_EOL;
}

// In the following examples, we will use var_dump to output the return value of isset().

$a = "test";
$b = "anothertest";

var_dump(isset($a));      // TRUE
var_dump(isset($a, $b)); // TRUE

unset ($a);

var_dump(isset($a));     // FALSE
var_dump(isset($a, $b)); // FALSE

$foo = NULL;
var_dump(isset($foo));   // FALSE
?>

Output:

Variable is set.
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)

This also works for elements in an array:

<?php
$a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple'));

var_dump(isset($a['test']));            // TRUE
var_dump(isset($a['foo']));             // FALSE
var_dump(isset($a['hello']));           // FALSE

// The key 'hello' is equal to NULL, so it is considered unset.
// To check for NULL key values, you can use the following method.
var_dump(array_key_exists('hello', $a)); // TRUE

// Deeper level check
var_dump(isset($a['pie']['a']));        // TRUE
var_dump(isset($a['pie']['b']));        // FALSE
var_dump(isset($a['cake']['a']['b']));  // FALSE
?>

Output:

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

Using isset() with String Offsets

PHP 5.4 changed the behavior of isset() when passed string offsets.

<?php
$expected_array_got_string = 'somestring';
var_dump(isset($expected_array_got_string['some_key']));
var_dump(isset($expected_array_got_string[0]));
var_dump(isset($expected_array_got_string['0']));
var_dump(isset($expected_array_got_string[0.5]));
var_dump(isset($expected_array_got_string['0.5']));
var_dump(isset($expected_array_got_string['0 Mostel']));
?>

Output in PHP 5.3:

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

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


Output of the above example in PHP 5.4:

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

Available PHP Functions

❮ Func Array Uintersect Func Array Natcasesort ❯