PHP 中“isset()”和“!empty()”有什么区别?
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 检查变量是否为空。空被解释为:""(空字符串)、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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP