Check whether an array is empty

Check whether an array is empty

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The empty() Operator and the Definition of an Empty Array

The code snippet you provided demonstrates a classic misunderstanding of how PHP's empty() operator evaluates arrays, specifically when dealing with array keys that have been explicitly assigned a value of false.

$error = array();
$error['something'] = false;
$error['somethingelse'] = false;

if (!empty($error))
{
    echo 'Error';
}
else
{
    echo 'No errors';
}

In your example, $error is initialized as an empty array. However, immediately after, you assign the boolean value false to two keys: 'something' and 'somethingelse'. Even though there are no truthy values in this array, the empty() function returns true (meaning !empty($error) is false), causing your script to print "No errors".

This behavior occurs because empty() does not simply check if an array contains at least one element. It checks for a specific set of conditions that define an array as "empty" in PHP's context. An array is considered empty by empty() if:

  1. The array has no keys (length is zero).
  2. The array contains only keys with values that are null, false, or 0.
  3. The array property is set but the underlying structure is uninitialized (though this rarely applies to standard arrays in modern contexts).

Your array satisfies the second condition perfectly. It has length of 2, but every single element is false. Therefore, PHP treats it as logically empty for control flow purposes. If your goal was to detect whether any actual work or data existed within that error list, you need a different approach than checking for logical emptiness.

Distinguishing Logical Emptiness from Structural Emptiness

The confusion often stems from treating empty() as a synonym for "is this array populated?". In strict programming terms, your array is populated; it holds two entries. However, semantically, those entries represent an absence of error information because they are explicitly set to false.

If you intended to verify that the $error array was never touched at all, checking its length is often a more robust strategy for structural validation:

if (count($error) > 0) {
    echo 'Error'; // Triggers here because count() sees 2 keys
} else {
    echo 'No errors';
}

Using count() ensures you are checking the physical size of the array rather than the logical value of its contents. This distinction is critical when building error reporting systems where an empty array might signify "no errors occurred," but an array filled with false flags might signify "errors were checked and found to be non-existent" or simply a design quirk where you pre-fill keys with defaults.

Handling null Values in Arrays

It is also worth noting how empty() behaves with null. If you initialized the array with null values instead of false, the behavior remains consistent regarding the operator's definition:

$error = [ 'something' => null, 'somethingelse' => null ];
var_dump(empty($error)); // bool(true)

In this scenario, the array is structurally non-empty (it has keys), but empty() returns true because the values are null. This is a known PHP behavior designed to allow developers to treat arrays containing only falsy values as if they were empty for conditional logic. While convenient, it can lead to bugs if you assume that !empty($array) guarantees the presence of data.

Best Practices for Error Flagging

When designing your error handling architecture, consider whether pre-filling an array with false is the right pattern. A common alternative is to keep the array truly empty until an error occurs:

$error = []; // Start truly empty

// Later in the logic
if ($somethingWentWrong) {
    $error['something'] = 'Critical failure occurred';
}

if (!empty($error)) {
    // Now we know for sure that at least one error was recorded
}

By reserving keys only when an actual error event happens, the empty() operator becomes a reliable sentinel. It accurately reflects whether any errors have been logged into the system. If you stick to the pattern of adding entries only on failure, !empty($error) will correctly return true only when there is something to report.

If you are working within the Laravel ecosystem, this pattern aligns well with how service containers and validation rules often handle error collections, ensuring that a collection is truly empty before attempting to render an error view. For more complex state management in Laravel applications, you might also consider using Eloquent's isEmpty() method on relationships or specific helper packages available at https://laravelcompany.com if you need additional utilities for managing array states in your application logic.

Ultimately, the choice between empty() and count() depends entirely on whether you care about the values inside the array or just its existence. In your specific case, since the values were false, empty() behaved exactly as designed, potentially masking the fact that the array keys were actively populated.

Our network