isset()和!empty()函数类似,两者都将返回相同的结果。但唯一的区别是!当变量不存在时,empty()函数不会生成任何警告或电子通知。它足以使用任何一个功能。通过将两个功能合并到程序中会导致时间流逝和不必要的内存使用。
1.isset()
isset ( mixed $var , mixed $... = ? ) : bool
var:要检查的变量。
返回值:如果 var 存在并且值不是 null 则返回 true,否则返回 false。
PS:如果已经使用 unset() 释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 null 的变量,将返回 false。同时要注意的是 null 字符("\0")并不等同于 PHP 的 null 常量。如果一次传入多个参数,那么 isset() 只有在全部参数都以被设置时返回 true 计算过程从左至右,中途遇到没有设置的变量时就会立即停止。
立即学习“PHP免费学习笔记(深入)”;
<?php $num = '0'; if( isset( $num ) ) { print_r(" $num is set with isset "); } echo "<br>"; // 声明一个空数组 $array = array(); echo isset($array['geeks']) ? 'array is set.' : 'array is not set.'; ?>
输出:
0 is set with isset functionarray is not set. array is not set.
2.empty()
empty ( mixed $var ) : bool
var:待检查的变量
返回值:当var存在,并且是一个非空非零的值时返回 false 否则返回 true.
<?php $temp = 0; if (empty($temp)) { echo $temp . ' is considered empty'; } echo "\n"; $new = 1; if (!empty($new)) { echo $new . ' is considered set'; } ?>
输出
0 is considered empty 1 is considered set
以下内容会被判定为空:
"" (空字符串)
0 (作为整数的0)
0.0 (作为浮点数的0)
"0" (作为字符串的0)
null
fals
earray() (一个空数组)
$var; (一个声明了,但是没有值的变量)
3.二者异同
isset()和!empty()函数类似,两者都将返回相同的结果。但唯一的区别是!当变量不存在时,empty()函数不会生成任何警告或电子通知。它足以使用任何一个功能。通过将两个功能合并到程序中会导致时间流逝和不必要的内存使用。
<?php $num = '0'; if( isset ( $num ) ) { print_r( $num . " is set with isset function"); } echo "\n"; $num = 1; if( !empty ( $num ) ) { print_r($num . " is set with !empty function"); } ?>
0 is set with isset function 1 is set with !empty function
以上就是PHP中的isset()和!empty()函数的异同的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号