Parameter must be an array or an object that implements Countable¶
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_arrayorinstanceof \Countable.
In more recent PHP versions, this error message is now must be of type Countable|array, %s given.
Static Analysis¶
This error may be tracked down with the following static analysis rules: Structures/CanCountNonCountable.