在PHP中,可以使用empty()
函數來判斷一個變量是否為空。empty()
函數會返回true
如果變量不存在或者值為false
、null
、空字符串""
、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