Common causes
- Passing null where an array or string is expected, often from a failed query or missing input
- Calling count() on null or a scalar, which became a TypeError in PHP 8.0
- Arithmetic on arrays or objects (Unsupported operand types)
- A typed function returning null without a nullable return type such as ?string
- declare(strict_types=1) rejecting a numeric string passed to an int parameter
- Old plugins or libraries not updated for PHP 8
How to fix it
- Read the message parts. Argument #1 ($items) must be of type array, null given names the parameter and the actual type. 'called in ... line 14' is the caller that passed the bad value; 'defined in' is the function itself.
- Fix the value at the caller. Find where the variable gets its value and make sure it is the expected type. For example, a database fetch that returns false or null when no rows are found must be handled before passing it on.
- Add safe defaults. Use count($items ?? []) or (string) ($value ?? '') where an empty value is acceptable. Use is_array() or is_countable() before calling count() on data you do not control.
- Correct type declarations. If null is a valid value, declare the parameter or return as nullable, such as ?array $items = null or : ?string. Use union types like int|string where both are legitimate.
- Update outdated code. If the trace points into a plugin, theme or vendor package, update it to a version that supports your PHP version. As a temporary fix, run the site on the older PHP version while you wait for an update.
Defensive fixes
<?php
// Before (TypeError on PHP 8 when $rows is null)
$total = count($rows);
// After
$total = is_countable($rows) ? count($rows) : 0;
// Nullable parameter and return types
function findName(?array $user): ?string {
return $user['name'] ?? null;
} How to stop it happening again
- Run static analysis (PHPStan or Psalm) to catch type mismatches before deployment
- Return empty arrays instead of null from functions that return lists
- Test plugins and libraries on your target PHP version before upgrading production
- Validate external input at the boundary and convert it to the expected types