How to check if an array is empty in PHP: 5 working methods

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to check if an array is empty in PHP? Five methods every developer should know

\n\n

Have you ever written a PHP script that needs to process an array of data, but suddenly encountered an "Undefined offset" or "Trying to access array offset on null" error? A familiar situation, right? Checking an array for emptiness is one of those basic but critically important operations that many beginner developers do incorrectly. In this article, we will break down how to check if an array is empty in PHP and look at all the nuances: from the simple use of empty() to comparison with []. Get your favorite code editor ready — there will be plenty of practice!

\n\n

Why is this important? A real-life story

\n\n

Imagine: you are writing an online store. A customer adds products to the cart and places an order. Your code must check if there are products in the cart before charging the money. If you incorrectly check the PHP array for emptiness, you will either charge for an empty cart (and get an angry call from the customer) or not process the order at all. That is exactly why the question "How to check whether an array is empty using PHP?" has thousands of views on Stack Overflow. Let's figure it out once and for all.

\n\n

Method 1. The empty() function — the simplest and most popular

\n\n

The empty() function in PHP is a Swiss army knife for checking empty values. It returns true if the variable does not exist, equals false, null, an empty string, 0, or an empty array. For checking an array for emptiness, this is the ideal option.

\n\n
<?php\n$emptyArray = [];\n$filledArray = ['apple', 'banana'];\n\nif (empty($emptyArray)) {\n    echo 'The array is empty!'; // Outputs: The array is empty!\n}\n\nif (!empty($filledArray)) {\n    echo 'The array contains elements: ' . implode(', ', $filledArray);\n    // Outputs: The array contains elements: apple, banana\n}\n?>
\n\n

Important: empty() does not generate a warning if the variable is not defined. This is safe and convenient. However, remember: if the array contains an element with the value 0 or an empty string '', empty() does not consider the entire array "empty" — it checks the array itself, not its elements.

\n\n

Method 2. The count() function — precise element counting

\n\n

If you need to know the exact number of elements, use count(). An array is considered empty if count($array) === 0. This method is especially useful when you want not only to check for emptiness but also to get the size of the array.

\n\n
<?php\n$users = ['Alice', 'Bob'];\n$emptyUsers = [];\n\nif (count($emptyUsers) === 0) {\n    echo 'The user list is empty.'; // Outputs: The user list is empty.\n}\n\necho 'Number of users: ' . count($users); // Outputs: 2\n?>
\n\n

Subtle point: count() will work even on a non-existent variable, but it will issue a warning Warning: count(): Parameter must be an array or an object that implements Countable. To avoid the error, combine it with isset() or is_array().

\n\n

Method 3. Comparison with an empty array (=== [])

\n\n

Sometimes the most straightforward method is the best. You can simply compare the variable with an empty array using strict comparison ===. This works quickly and is intuitively clear.

\n\n
<?php\n$data = [];\n\nif ($data === []) {\n    echo 'The data array is empty.'; // Outputs: The data array is empty.\n}\n\n$config = ['debug' => true];\nif ($config !== []) {\n    echo 'The configuration is not empty.';\n}\n?>
\n\n

Pros: The code reads like plain English. Cons: If the variable is not defined, you will get a fatal error Undefined variable. Therefore, use thi

Blogs

Book Recommendations