What changes were made in PHP 7.2 that led to the warning being thrown when count() is used on non-array or non-Countable objects?
In PHP 7.2, changes were made to the count() function to throw a warning when used on non-array or non-Countable objects to prevent potential errors. To solve this issue, you can check if the variable is an array or implements the Countable interface before using count().
if (is_array($variable) || $variable instanceof Countable) {
$count = count($variable);
} else {
$count = 0;
}
Keywords
Related Questions
- What are some best practices for troubleshooting SQL syntax errors in PHP?
- How can the order of execution of PHP files impact the display and functionality of a webpage with multiple modules?
- What are the best practices for managing session IDs in a PHP application, especially in a shopping cart scenario?