Home/ Engineering Insights/ Fatal Error: count(): Argument #1 Must Be of Type Countable|Array in PHP 8

Fatal Error: count(): Argument #1 Must Be of Type Countable|Array in PHP 8

Source. This article is based on PHP manual: count(). Details were checked against it on November 4, 2026; check the source for later changes.

It is one of the most common errors after a move to PHP 8. A page that worked for years now stops with:

PHP Fatal error:  Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given

The value can also be false or a string. The cause is a change in how count() treats anything that is not an array or a Countable object. The manual's changelog for the function lists two steps: PHP 7.2.0 started yielding a warning on invalid countable types, and PHP 8.0.0 throws a TypeError instead.

Why old code got away with it

Before PHP 7.2, counting something that was not countable returned a number instead of failing: 0 for null and 1 for most other values, including false. Code like this worked, by accident:

$rows = get_rows($category);   // returns false when the query fails
if (count($rows) > 0) {
    // show the rows
}

A failed lookup returned false, count(false) gave 1, and the page carried on as if there was one row. Many mysterious bugs in old code are exactly this. PHP 8 stops the request instead, which is better for correctness and harder on legacy sites.

Find every case before production does

You do not have to wait for PHP 8. From PHP 7.2 the same bugs produce a warning, so a staging copy on PHP 7.2, 7.3 or 7.4 with full error reporting lists them all:

php.ini on the staging copy
error_reporting = E_ALL
display_errors = Off
log_errors = On

grep "count()" php-error.log | sed -E 's/^\[[^]]*\] //' | sort | uniq -c | sort -rn | head -40

On PHP 8 the same search finds the TypeError lines, one request at a time, which is slower. Finding them on 7.x first is cheaper.

Where the bad value usually comes from

Once a hit is in front of you, trace the variable backwards until you find where it stopped being an array. In legacy code the same few causes come up again and again:

  • A database helper that returns false when a query fails, and a caller that never checks.
  • A variable assigned in only one branch of an if, so the other branch leaves it unset.
  • A function with an early return; that hands back null on one path.
  • Decoded input: json_decode() returns null when the text is not valid JSON, and an API that is down gives you exactly that.
  • A form or query-string value that was not submitted, so the key does not exist.

The count is only where the problem becomes visible. If you fix it at the count with a default, you keep the page alive but also keep whatever hid the failure. Decide for each case whether "nothing to count" is a normal outcome, in which case a default is fine, or a failure that should be logged.

Fixing it: start at the source

The right fix depends on why the value is not an array. Work through these in order.

  1. Initialise. If the variable is only sometimes filled, give it an empty array first: $items = [];.
  2. Handle failure where it happens. Functions such as json_decode(), file() and glob() return null or false when they fail, and so do many home-grown database helpers. Check for that and decide what the page should do, instead of counting the result.
  3. Guard the count. is_countable($value) arrived in PHP 7.3, per the manual. On older versions use is_array($value). For a quick default, count($value ?? []) handles null (the ?? operator needs PHP 7.0), but not false.
  4. Be careful with casts. count((array) $value) turns null into an empty array, but turns false into an array holding one element, so a failed lookup still counts as 1. That keeps the old bug.

A typical repair handles the failure explicitly:

$rows = get_rows($category);
if ($rows === false) {
    // log the failure and show a clear message
    $rows = [];
}
if (count($rows) > 0) {
    // show the rows
}

What not to do

Do not wrap every call in @ or silence the error log. A hidden count() problem usually means a failed query or a missing input further up, and that is the thing to fix. Lowering error_reporting on PHP 8 does not help either: a TypeError is an exception, not a notice.

Test and move on

After the fix, run the pages that use the code with both a normal result and an empty or failed one. That second case is the one old code never tested. Our guide to adding tests to legacy PHP code shows how to capture this behaviour cheaply. We have not run these changes on your code, so verify them on a copy.

If this error is one of many after an upgrade, the underlying cause is often weak error handling in the data layer. Our legacy system support work covers that. Related errors from the same upgrade: Call to undefined function mysql_connect(), old-style constructors that never run and undefined variable and array key warnings.

Frequently asked questions

What is the difference between PHP 7.2 and PHP 8 for count()?

In PHP 7.2 count() on a value that is not countable gives a warning. In PHP 8.0 it throws a TypeError, according to the changelog in the manual.

Does count(null) work in PHP 8?

No, it throws a TypeError. Use count($x ?? []) or check that the value is an array first.

Is is_countable() available on PHP 5.6?

No. is_countable() was added in PHP 7.3. On older versions use is_array() or an instanceof Countable check.

Errors appearing all over after a PHP upgrade?

We find the root causes, fix them and test the platform on a copy before it moves to a supported PHP version.

See our Legacy System Support

Read about our legacy PHP modernisation services →

Ravi Raghav, Founder & CEO of AllScript Lab
Written by Ravi Raghav Founder & CEO