温馨提示×

如何在PHP中判断数组类型

PHP
小樊
81
2024-09-12 11:35:46
栏目: 编程语言

在 PHP 中,可以使用 is_array() 函数来判断一个变量是否为数组类型

<?php
$array = array("apple", "banana", "cherry");
$notArray = "This is not an array.";

if (is_array($array)) {
  echo "The variable '\$array' is an array.";
} else {
  echo "The variable '\$array' is not an array.";
}

if (is_array($notArray)) {
  echo "The variable '\$notArray' is an array.";
} else {
  echo "The variable '\$notArray' is not an array.";
}
?>

上述代码将输出:

The variable '$array' is an array.
The variable '$notArray' is not an array.

通过使用 is_array() 函数,您可以判断给定的变量是否为数组类型。如果该变量是数组,则函数返回 true;否则,返回 false

0