What are some common mistakes to avoid when using count() function in PHP?

One common mistake to avoid when using the count() function in PHP is not checking if the input is an array or a countable object before calling the function. This can result in errors or unexpected behavior if the input is not valid for counting. To solve this issue, always ensure that the input is an array or a countable object before using the count() function.

// Check if the input is an array or a countable object before using count()
if (is_array($input) || $input instanceof Countable) {
    $count = count($input);
    // Use the count value here
} else {
    // Handle the case when input is not valid for counting
    echo "Input is not an array or countable object";
}