PHP isset() Function

PHP 可用的函数PHP Available Functions

isset()The function is used to detect whether a variable is set and is not NULL.

If a variable has been released using unset(), then checking it with isset() will return FALSE.

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

Also note that the null character ("\0") is not equivalent to PHP's NULL constant.

PHP Version Requirements: 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() returns TRUE only if all parameters are set. The evaluation proceeds from left to right and stops immediately when it encounters an unset variable.

Return Value

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

Example

Example

<?php $var = ''; //The result is TRUE, so the following text will be printed. if (isset($var)) { echo "The 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 ?>

The output is:

变量已设置。
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)

This also works for elements in an array:

Example

<?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 key 'hello' is equal to NULL, so it is considered unset. //If you want to check NULL key values, you can try the method below. 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 ?>

The output is:

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

Using isset() on String Offsets

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

Example

<?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 output of the above example in PHP 5.3:

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

The output of the above example in PHP 5.4:

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

PHP 可用的函数PHP Available Functions

Other Extensions