Ad Space — Top Banner

Warning: count() expects parameter 1 to be array or countable

PHP Programming Language

Severity: Minor

What Does This Error Mean?

This warning means you called count() on something that is not an array or countable object — like null, a string, or a number. In PHP 7.2, passing non-countable values to count() started generating this warning. Count() returns 1 for non-arrays (it used to silently count null as 0) — which is often wrong. The fix is to check that the variable is an array before counting it.

Affected Models

  • PHP 7.2 and later
  • PHP 8.x

Common Causes

  • Calling count() on a variable that might be null when a query or function returns no results
  • Calling count() on a string, number, or boolean value
  • A function that should return an array sometimes returns null or false on failure
  • Passing a stdClass object to count() — objects need to implement Countable to be countable
  • Calling count() before checking if a result set is valid

How to Fix It

  1. Check if the variable is an array before counting it: $count = is_array($items) ? count($items) : 0;

    This returns 0 if $items is not an array, instead of generating a warning or returning a wrong count.

  2. Use a null-safe fallback: $count = count($items ?? []) — if $items is null, ?? [] gives count() an empty array, which returns 0 safely.

    The ?? (null coalescing) operator returns the right side only when the left side is null. This is a clean one-liner fix.

  3. When getting results from a database or API, always initialize the variable as an empty array: $items = []; before the query, then populate it if the query succeeds.

    This ensures $items is always an array (even empty) rather than possibly null — count([]) returns 0, which is correct.

  4. For database results, check the return value before counting: $rows = $stmt->fetchAll(); $count = count($rows); — fetchAll() always returns an array (empty if no results), so this is safe.

    Unlike fetch() which returns false on no results, fetchAll() returns an empty array — making count() safe to use directly.

  5. If counting items in an if() condition, use the same approach: if (!empty($items)) — empty() returns true for empty arrays, null, 0, false, and empty string.

    empty() is often more appropriate than count() > 0 because it handles null and empty arrays with one check.

When to Call a Professional

This warning is always fixable yourself. Check that the value is an array before counting, or use a fallback empty array. This warning was added in PHP 7.2 to catch code that was silently misbehaving before.

Frequently Asked Questions

What did count() return on null before PHP 7.2?

Before PHP 7.2, count(null) silently returned 0 without any warning. This was considered a bug because it masked real errors — code assumed it had an array but actually had null. PHP 7.2 added the warning to expose this pattern so developers could fix their code. In PHP 8.0, passing null to count() was deprecated, and PHP 8.1+ treats it as an error.

What is a Countable interface in PHP?

Countable is a built-in PHP interface for objects that can be counted. If a class implements Countable, it provides a count() method and PHP's count() function will use it. For example: class MyCollection implements Countable { public function count(): int { return count($this->items); } } Now count(new MyCollection()) works without a warning.

What is the difference between count() and sizeof()?

sizeof() is an alias for count() — they do exactly the same thing. There is no functional difference between them in PHP. Most modern PHP code uses count() since it is the primary function name. sizeof() exists because it mirrors a function in C, making the language feel familiar to C programmers.