- 格式:bool isset ( mixed var [, mixed var [, ...]] )
- 功能:检测变量是否设置
- 返回值:
- 若变量不存在则返回 FALSE
- 若变量存在且其值为NULL,也返回 FALSE
- 若变量存在且值不为NULL,则返回 TURE
- 同时检查多个变量时,每个单项都符合上一条要求时才返回 TRUE,否则结果为 FALSE
- 版本:PHP 3, PHP 4, PHP 5
更多说明:
- 使用 unset() 释放变量之后,它将不再是 isset()。
- isset() 只能用于变量,传递任何其它参数都将造成解析错误。
- 检测常量是否已设置可使用 defined() 函数。
<?php
$var = '';
if (isset($var)) // 空值、0、false的赋值结果均被isset判为 TRUE,所以后边的文本将被打印出来。
print "blank value -> isset = true. ";
$var = NULL;if (!isset($var)) // NULL将被isset判为 FALSE
print "NULL value -> isset = false. "; //以下使用 var_dump 输出 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
?>
isset()同样适用于数组元素和对象元素的检查。若数组或对象实例未被定义,则检测其中的数组元素/对象元素将被返回false。
<?php
$a = array ('test' => 1, 'hello' => NULL);
var_dump( isset ($a['test']) ); // TRUE
var_dump( isset ($a['foo']) ); // FALSE
var_dump( isset ($a['hello']) ); // FALSE
// 键 'hello' 的值等于 NULL,所以被认为是未置值的。
// 如果想检测 NULL 键值,可以试试下边的方法。
var_dump( array_key_exists('hello', $a) ); // TRUE
?>
注: 由于这是一个语言结构而非函数,因此它无法被变量函数调用。
empty()、unset()、defined()、array_key_exists() 和错误控制 @ 运算符。
http://www.allwiki.com/wiki/PHP%E7%9A%84isset()%E5%87%BD%E6%95%B0 天下维客