4 Ways to Check if an Array is Empty in PHP

Updated: January 10, 2024 By: Guest Contributor Post a comment

Introduction

Working with arrays is a fundamental aspect of PHP programming. Whether you’re managing form inputs, handling database records, or simply storing data, it’s often necessary to determine if an array is empty. PHP offers several methods to perform this check, each with its own usage contexts and implications. This guide will walk you through various solutions for empty array checking in PHP.

Solution 1: Using empty()

The empty() function is PHP’s dedicated tool for checking whether a variable is empty. An array is considered “empty” if it does not exist, or if it’s an empty array (has zero elements).

  1. Run the empty() function on the array variable.
  2. Check the boolean result returned by the function.

Example:

$array = [];
if (empty($array)) {
    echo 'The array is empty';
} else {
    echo 'The array is not empty';
}
// Output: The array is empty

Performance Discussion: The empty() function is quick and efficient, especially since it’s a language construct and not a function.

Advantages: It’s straightforward and doesn’t throw a warning if the variable doesn’t exist.

Limitations: It also checks if the variable is set, which might not be desirable in all cases.

Solution 2: Counting Elements with count()

The count() function returns the number of elements in an array. If the array is empty, count() will return 0.

  1. Use the count() function to determine the array’s size.
  2. Compare the result to 0 to establish if the array is empty.

Example:

$array = [];
if (count($array) === 0) {
    echo 'The array is empty';
} else {
    echo 'The array is not empty';
}
// Output: The array is empty

Performance Discussion: This method introduces some overhead compared to empty(), especially for large arrays. However, performance is typically not an issue for small to medium-sized arrays.

Advantages: Explicitly checks for an array’s element count.

Limitations: Slightly slower than empty() (only noticeable with extremely large arrays).

Solution 3: Using sizeof()

The sizeof() function is an alias to count() and operates in the same way.

  1. Apply the sizeof() function on the array.
  2. Examine if the returned value is 0.

Example:

$array = [];
if (sizeof($array) === 0) {
    echo 'The array is empty';
} else {
    echo 'The array is not empty';
}
// Output: The array is empty

Advantages: Equal to count(), with the same pros and cons.

Solution 4: Comparing to an Empty Array

Directly compare the array against an empty array to determine if it is empty.

  1. Create an if statement comparing the array to an empty array [].
  2. Analyze if the arrays are identical.

Example:

$array = [];
if ($array === []) {
    echo 'The array is empty';
} else {
    echo 'The array is not empty';
}
// Output: The array is empty

Advantages: Simple syntax and clear intent.

Limitations: Mostly syntactic sugar, with no performance benefit over empty().

Conclusion

Checking whether an array is empty in PHP is crucial in validating data and controlling the flow of your application. The empty() function is the more intuitive and quicker solution, while count() and sizeof() provide an explicit check for the number of elements, which could be more suitable in situations where you only want to check for emptiness without the concern of the variable’s existence. Lastly, comparing the array directly to an empty array syntax is the most readable form, though it offers no clear benefits over the empty() function. When writing your code, weigh the context and choose the solution that best suits your needs.