English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP isset() Function Usage and Examples

PHP available functions

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

If a variable has been released using unset() and then judged by isset(), it will return FALSE.

If isset() is used to test a variable that has been set to NULL, it will return FALSE.

At the same time, it should be noted that the null character ("\0") is not equal to PHP's NULL constant.

PHP Version Requirement: PHP 4, PHP 5, PHP 7

Syntax

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

Parameter Description:

  • $var: The variable to be checked.

If multiple parameters are passed at once, isset() will only return TRUE if all parameters are set. The calculation process is from left to right, and it will stop immediately if it encounters a variable that is not set.

Return Value

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

Online Examples

<?php
$var = '';
// The result is TRUE, so the following 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().
// 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 result is:

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

This also applies to elements within 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 value of the key 'hello' is NULL, so it is considered to be unset
// If you want to check for NULL key values, you can try the following method. 
var_dump(array_key_exists('hello', $a)); // TRUE
// Deeper level detection
var_dump(isset($a['pie']['a']));        // TRUE
var_dump(isset($a['pie']['b']));        // FALSE
var_dump(isset($a['cake']['a']['b']));  // FALSE
?>

Output result is:

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

Using isset() with string offset

PHP 5.4 Changed the behavior of isset() when used with string offset.

<?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']));
?>

The above examples in PHP 5.3 Output in:

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

The above examples in PHP 5.4 Output in:

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

PHP available functions