Is Array Empty in PHP? Exploring All Methods of Checking

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? A Complete Guide with Examples

\n\n

Hello, fellow developer! If you've ever written code in PHP, you've surely encountered a situation where you need to figure out: does the array have anything in it, or is it as empty as a student's pocket after exams? This task seems trivial, but it hides several pitfalls. Especially for beginners who might accidentally crash a script due to an incorrect check.

\n\n

Today we'll explore all popular ways to check for an empty array in PHP, from the simplest to the professional ones. You'll learn how empty() differs from isset(), why count() can be dangerous, and how to write perfect code. Let's go!

\n\n

Why is this important? Typical beginner mistakes

\n\n

Imagine: you receive data from a user (for example, a list of items in a cart). If the array is empty — you need to show a message "Cart is empty". If it's not empty — display the items. It seems like simple logic. But here's what beginners often write:

\n\n
// Mistake #1: comparison with false\nif ($cart == false) {\n    echo "Cart is empty";\n}\n\n// Mistake #2: implicit type casting\nif ($cart) {\n    // This code will execute if the array is not empty\n}
\n\n

Seems to work? Yes, but only in simple cases. But what if the array has keys, but all values are null or empty strings? Or if the variable is not defined at all? That's where bugs start. To avoid them, let's break down checking an empty array in PHP properly.

\n\n

Method #1: The empty() function — the most popular

\n\n

This is the first thing that comes to mind. The empty() function in PHP returns true if the variable is empty. For an array, this means: the array does not exist, equals null, or contains no elements.

\n\n
<?php\n$emptyArray = [];\n$nonEmptyArray = ['apple', 'banana'];\n\nvar_dump(empty($emptyArray));    // true\nvar_dump(empty($nonEmptyArray)); // false\n?>
\n\n

Pros: short, readable, works for undefined variables (doesn't throw a warning).

\n\n

Cons: empty() also considers 0, '0', '', false, null, and an empty SimpleXMLElement object as empty. If your array contains, for example, one element with the value 0 — empty() will say it's empty. And that's not always what you need.

\n\n
<?php\n$trickyArray = [0];\nvar_dump(empty($trickyArray)); // true! But there is an element\n?>
\n\n

Conclusion: use empty() only when you are absolutely sure that the array won't contain "falsy" values (0, empty string, etc.). For most tasks with forms and user input, it's an excellent option.

\n\n

Method #2: The count() function — precise counting

\n\n

The second most popular way is checking an array for emptiness using count(). This function returns the number of elements in an array.

\n\n
<?php\n$array1 = [];\n$array2 = ['a' => 1, 'b' => 2];\n\nif (count($array1) === 0) {\n    echo "Array is empty";\n}\n\nif (count($array2) > 0) {\n    echo "Array is not empty, it has " . count($array2) . " elements";\n}\n?>
\n\n

Pros: absolutely accurate result. If count() returns 0 — there are no elements in the array. No surprises with falsy values.

\n\n

Cons: for very large arrays, count() can be slower than empty() (though the difference is usually negligible). Also, you need to be careful: if the variable is not defined, count() will throw a warning. Use a check with isset() or @count() (but suppressing errors is bad practice).

\n\n

Best practice: combine with isset():

\n\n
<?php\nif (isset($array) && count($array)                

Blogs

Book Recommendations