首页 文章资讯内容详情

PHP中的isset()和!empty()有什么区别?

2026-06-04 1 花语

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检查变量是否为空。空解释为:“”(空字符串),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