要使用PHP的is_array()
函數檢查一個變量是否為數組,只需將該變量作為參數傳遞給函數
<?php
$variable1 = array("apple", "banana", "cherry");
$variable2 = "This is a string";
if (is_array($variable1)) {
echo "Variable 1 is an array.";
} else {
echo "Variable 1 is not an array.";
}
if (is_array($variable2)) {
echo "Variable 2 is an array.";
} else {
echo "Variable 2 is not an array.";
}
?>
在這個例子中,$variable1
是一個數組,所以輸出結果是"Variable 1 is an array.“。而$variable2
是一個字符串,所以輸出結果是"Variable 2 is not an array.”。