PHP 中 'isset()' 和 '!empty()' 有什么区别?
phpserver side programmingprogramming更新于 2025/10/1 17:52:17
Isset 函数
ISSET 检查变量是否已设置。换句话说,它检查变量是否为除 NULL 以外的任何值或未赋值。如果变量存在且具有除 NULL 以外的值,则 ISSET 返回 TRUE。这意味着赋值为"""、"0"、"0""或 FALSE 的变量已设置,因此对于 ISSET 来说为 TRUE。
示例
<?php
$val = '0';
if( isset($val)) {
print_r(" $val is set with isset function <br>");
}
$my_array = array();
echo isset($my_array['New_value']) ?
'array is set.' : 'array is not set.';
?>
输出
这将产生以下输出 −
0 is set with isset function array is not set.
!empty 函数
EMPTY 用于检查变量是否为空。Empty 会被解释为:""(空字符串)、0(整数)、0.0(浮点数)、"0"(字符串)、NULL、FALSE、array()(空数组)以及 "$var;"(已声明但在类中没有值的变量)。
示例
<?php
$temp_val = 0;
if (empty($temp_val)) {
echo $temp_val . ' is considered empty';
}
echo "nn";
$new_val = 1;
if (!empty($new_val)) {
echo $new_val . ' is considered set';
}
?>
输出
这将产生以下输出 −
0 is considered empty 1 is considered set

