PHP Check Empty Array: Easy Methods Explained

You can determine if an array is empty by using the following method:

  1. The empty() function is used to check if a variable is empty, and can be used to check if an array is empty. It returns a boolean value – true if the array is empty, false if it is not. For example:
if (empty($array)) {
    echo '数组为空';
} else {
    echo '数组不为空';
}
  1. Using the count() function: The count() function is used to retrieve the number of elements in an array. If the number of elements in the array is 0, it means the array is empty. For example:
if (count($array) == 0) {
    echo '数组为空';
} else {
    echo '数组不为空';
}
  1. The use of the sizeof() function: The sizeof() function serves the same purpose as the count() function, which is to obtain the number of elements in an array. For example:
if (sizeof($array) == 0) {
    echo '数组为空';
} else {
    echo '数组不为空';
}

No matter which method is used, it is possible to determine if an array is empty.

bannerAds