Encodera

Thanks for reading!

Donate to the site — we work hard for you.

How to check if an array is empty

Short answer

Compare the array with an empty array:

<?php
 
$items = [];
 
var_dump($items === []);

Output:

bool(true)

With count

The number of elements is zero:

<?php
 
$items = [];
 
var_dump(count($items) === 0);

Output:

bool(true)

In a condition

An empty array counts as false in a condition, so the check can be written briefly:

<?php
 
$items = [];
 
echo $items ? 'has items' : 'empty';

Output:

empty

Functions used in this recipe

  • count — Returns the number of elements in an array.
  • empty — Checks that a variable does not exist or holds an "empty" value.
  • is_array — Checks whether a value is an array.

Thanks for reading!

Donate to the site — we work hard for you.