must be of type Countable|array, %s given

Description

The count() native function only count elements that are arrays, or objects that implements the Countable interface. Anything else yields a fatal error.

Example

<?php

$value = null;
echo count($value);

?>

Solutions

  • Check for type before using count: is_array or instanceof \Countable.

In previous PHP versions, this error message used to be Parameter must be an array or an object that implements Countable.

Static Analysis

This error may be tracked down with the following static analysis rules: Structures/CanCountNonCountable.