Uncaught TypeError: count() in File /usr/local/www/head.inc
This error occurs when the count() function in PHP is used on a variable that is not an array or countable object. In the case of the pfSense file head.inc, the variable $notices is passed to count() but may sometimes be false or another non-Countable type, resulting in the following error:
PHP Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given in /usr/local/www/head.inc on line 536
To fix this issue, ensure the $notices variable is validated before using it with count(). Update the code to handle non-countable values gracefully.
-
Locate the line
Open the file
/usr/local/www/head.incand find the following line:<span class="badge bg-danger"><?=count($notices)?></span>
-
Replace with a Safe Alternative
Replace it with code that checks if $notices is Countable before calling count():
<span class="badge bg-danger"><?=is_countable($notices) ? count($notices) : 0?></span>The $notices variable is populated by the get_notices() function. This function might return false or a non-countable type under certain conditions, which causes the error when count() is called.
What could cause $notices to be non-countable?
If get_notices() returns false when there are no notices.
If $notices is not initialized properly in some cases.