温馨提示×

php empty函数判断变量为空的方法

PHP
小樊
90
2024-08-18 16:32:37
栏目: 编程语言

在PHP中,可以使用empty()函数来判断一个变量是否为空。empty()函数会返回true如果变量不存在或者值为falsenull、空字符串""0或者空数组array(),否则返回false

以下是一个使用empty()函数判断变量是否为空的示例:

$var1 = "hello";
$var2 = "";
$var3 = 0;
$var4 = null;

if (empty($var1)) {
    echo "var1 is empty";
} else {
    echo "var1 is not empty";
}

if (empty($var2)) {
    echo "var2 is empty";
} else {
    echo "var2 is not empty";
}

if (empty($var3)) {
    echo "var3 is empty";
} else {
    echo "var3 is not empty";
}

if (empty($var4)) {
    echo "var4 is empty";
} else {
    echo "var4 is not empty";
}

以上代码会输出:

var1 is not empty
var2 is empty
var3 is empty
var4 is empty

0