Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP isset() Function

PHP isset() Function

PHP Available FunctionsAvailable PHP Functions

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

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

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

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

PHP version requirement: PHP 4, PHP 5, PHP 7

Syntax


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

Parameter Description:

If multiple parameters are passed at once, isset() returns TRUE only when all parameters are set. The evaluation proceeds from left to right and stops immediately when an unset variable is encountered.

Return Value

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

Example

Example

The output is:


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

This also works for elements in an array:

Example

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 // key 'hello' has a value equal to NULL, so it is considered unset // if you want to detect a NULL key value, you can try the method below. var_dump ( array_key_exists ( ' hello ' , $a ) ) ; // TRUE // deeper 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() with string offsets

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

Example

The output of the example above in PHP 5.3:


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

The output of the example above in PHP 5.4:


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

PHP Available FunctionsAvailable PHP Functions