1.isset()
isset ( mixed $var , mixed $... = ? ) : bool PS:如果已经使用 unset() 释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 null 的变量,将返回 false。同时要注意的是 null 字符("\0")并不等同于 PHP 的 null 常量。如果一次传入多个参数,那么 isset() 只有在全部参数都以被设置时返回 true 计算过程从左至右,中途遇到没有设置的变量时就会立即停止。 <?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 <?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视频教程》《php教程》 以上就是PHP中的isset()和!empty()函数的异同的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |