empty()是一个PHP内置函数,用于判断一个变量是否为空。它返回一个布尔值,当变量为以下情况之一时返回true:
以下是一些empty()函数的实例:
$var1 = ""; // 空字符串
$var2 = 0; // 数值0
$var3 = null; // null
$var4 = false; // 布尔值false
$var5 = array(); // 空数组
$var6 = new stdClass(); // 没有任何属性的对象
// 使用empty()函数检查变量是否为空
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.";
}
if (empty($var5)) {
echo "var5 is empty.";
} else {
echo "var5 is not empty.";
}
if (empty($var6)) {
echo "var6 is empty.";
} else {
echo "var6 is not empty.";
}
输出结果为:
var1 is empty.
var2 is empty.
var3 is empty.
var4 is empty.
var5 is empty.
var6 is empty.
根据输出结果可以看出,当变量满足empty()函数的条件时,返回true,否则返回false。