Checking if all the array items are empty PHP

Checking if all the array items are empty PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Checking if all the array items are empty PHP

Handling form validation in PHP often involves checking multiple fields to ensure data integrity. A common pattern arises when you have a set of optional fields, and your business logic requires at least one of them to be populated before proceeding. In this scenario, you need to distinguish between a user who intentionally left all fields blank versus one who partially filled the form.

Consider an array constructed from $_POST data where specific fields like RequestID, ClientName, and Status are expected:

$array = array(
    'RequestID'       => $_POST["RequestID"],
    'ClientName'      => $_POST["ClientName"],
    'Username'        => $_POST["Username"],
    'RequestAssignee' => $_POST["RequestAssignee"],
    'Status'          => $_POST["Status"],
    'Priority'        => $_POST["Priority"]
);

The goal is to trigger an error message—such as <li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>—only if every element in this array evaluates to false (empty strings, null, or non-existent keys).

The Naive Approach and Its Pitfalls

A frequent mistake is iterating through the array and checking if each item is empty using a simple loop. However, this approach often fails when dealing with $_POST data directly because accessing an unset key results in null, not an empty string. If you simply check if ($array[$key] == ''), it won't catch null values.

Furthermore, relying on count(array_filter($array)) === 0 works well for checking emptiness but has a specific behavior with $_POST. By default, $_POST is an indexed array of strings (or nulls). If you are building the $array manually as shown above, you might end up with keys that hold null if the input field was not submitted at all.

If your goal is strictly to check if the values are empty, you must account for both empty strings and null values. The most robust way to do this without external libraries is using array_filter combined with a callback or checking the count of non-empty items.

Using array_filter for Robustness

The cleanest native PHP solution involves filtering out "falsy" values and checking if anything remains. You can define what constitutes an empty value explicitly. An empty string '', a null value null, and a false boolean are typically the indicators of missing data in this context.

Here is how you can implement this logic:

$nonEmptyItems = array_filter($array, function ($value) {
    return $value !== '' && $value !== null;
});

if (count($nonEmptyItems) === 0) {
    $error_str .= '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>';
}

This approach is explicit and readable. The array_filter function removes elements that do not pass the callback test. If the resulting array has a count of zero, it means every single item in the original $array was considered empty or non-existent.

Handling the Source Directly

In many cases, you might not need to construct an intermediate $array. You can apply the logic directly to $_POST. This reduces memory usage and simplifies the code flow. However, you must be careful with keys that do not exist at all. Accessing $_POST['RequestID'] on a missing key returns null, which your filter logic handles correctly by treating it as empty.

$nonEmptyItems = array_filter($_POST, function ($value) {
    // Check for empty string, null, or false
    return $value !== '' && $value !== null;
});

if (count($nonEmptyItems) === 0) {
    $error_str .= '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>';
}

Trade-offs and Considerations

When implementing this check, consider the source of your data. If these fields come from a database query rather than $_POST, you might have arrays containing numeric strings or boolean values mixed in. The logic !== '' handles numeric strings (like "5") correctly as valid data, but it treats an empty string "" and null null as invalid. This distinction is crucial for validation layers where whitespace-only strings are also often undesirable.

If you prefer a more modern, functional style or are using PHP 8+, you could utilize the array_some concept implicitly by checking if the filtered array is empty. While PHP does not have a built-in "is all empty" function in the core language prior to recent iterations, the pattern of filtering and counting remains the standard idiomatic solution.

For developers working within the Laravel ecosystem, similar validation rules are often handled using the Validator facade or Form Requests, which abstracts away these manual checks. However, understanding the underlying mechanism helps when debugging custom forms or integrating with legacy systems where such a specific check is required in raw PHP.

Our network