小白教程
所有教程
关于
Search
172.69.58.242
172.69.58.242
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Php isset function”的源代码
本页内容
上一节:
Php_is_string_function
下一节:
Php_print_r_function
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:isset}}[[Category:php variable handling functions|27]] = PHP isset() 函数 = [[php variable handling functions|PHP 可用的函数]] '''isset()''' 函数用于检测变量是否已设置并且非 NULL。 如果已经使用 unset() 释放了一个变量之后,再通过 isset() 判断将返回 FALSE。 若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。 同时要注意的是 null 字符("\0")并不等同于 PHP 的 NULL 常量。 PHP 版本要求: PHP 4, PHP 5, PHP 7 === 语法 === <sample title="" desc="" lang="php" hererun="1"> bool isset ( mixed $var [, mixed $... ] ) </sample> 参数说明: * $var:要检测的变量。 如果一次传入多个参数,那么 isset() 只有在全部参数都被设置时返回 TRUE,计算过程从左至右,中途遇到没有设置的变量时就会立即停止。 === 返回值 === 如果指定变量存在且不为 NULL,则返回 TRUE,否则返回 FALSE。 == <sample title="" desc="" lang="php" hererun="1"> <?php $var = ''; // 结果为 TRUE,所以后边的文本将被打印出来。 if (isset($var)) { echo "变量已设置。" . PHP_EOL; } // 在后边的例子中,我们将使用 var_dump 输出 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 ?> </sample> 输出结果为: <sample title="" desc="" lang="php" hererun="1"> 变量已设置。 bool(true) bool(true) bool(false) bool(false) bool(false) </sample> 这对于数组中的元素也同样有效: <sample title="" desc="" lang="php" hererun="1"> <?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 // 键 'hello' 的值等于 NULL,所以被认为是未设置的 // 如果想检测 NULL 键值,可以试试下边的方法。 var_dump(array_key_exists('hello', $a)); // TRUE // 更深层次检测 var_dump(isset($a['pie']['a'])); // TRUE var_dump(isset($a['pie']['b'])); // FALSE var_dump(isset($a['cake']['a']['b'])); // FALSE ?> </sample> 输出结果为: <sample title="" desc="" lang="php" hererun="1"> bool(true) bool(false) bool(false) bool(true) bool(true) bool(false) bool(false) </sample> === 在字符串位移中使用 isset() === PHP 5.4 改变了传入字符串位移时 isset() 的行为。 <sample title="" desc="" lang="php" hererun="1"> <?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'])); ?> </sample> 以上实例在 PHP 5.3 中的输出: <sample title="" desc="" lang="php" hererun="1"> bool(true) bool(true) bool(true) bool(true) bool(true) bool(true) </sample> 以上实例在 PHP 5.4 中的输出: <sample title="" desc="" lang="php" hererun="1"> bool(false) bool(true) bool(true) bool(true) bool(false) bool(false) </sample> [[php variable handling functions|PHP 可用的函数]]
返回至“
Php isset function
”。
上一节:
Php_is_string_function
下一节:
Php_print_r_function