Check if PHP array is empty using count()

June 21, 2022  ‐ 1 min read

Checking whether an array is empty comes down to checking whether there are any elements in the array. Therefore a logical approach is to count the elements in the array and check if this number is equal to zero.

<?php

$arr = [];
$isEmpty = count($arr) === 0;

But, since empty arrays in PHP are considered 'falsy' we can just check whether it evaluates to true or false in a boolean context.

<?php

$arr = [];

if (!$arr)
{
  echo 'Empty';
}