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。