How to check if an array is empty in PHP?

To check if an array is empty in PHP your best bet is to count the number of items that contains. To do that you need to simply apply the count function on your array. Let’s try that using the code below:

$arr = array();
if (count($arr) == 0) {
echo 'empty';
}

The count function however may not be great for performance if the data is too large so you may need to use a safer way using the negation operator ( ! ). If an array is empty, PHP will return false and that is what we will be looking for using the following code.

$arr = array();
if (!$arr)  {
echo 'empty';
}