So I upgraded to PHP 8 and ran my script which gave me this error:
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in C:\xampp\htdocs\app\includes\functions\create_session.php:78
Stack trace:
#0 C:\xampp\htdocs\public\front_desk.php(508): Session->check_subfeature_access(22, 0)
#1 {main} thrown in C:\xampp\htdocs\app\includes\functions\create_session.php on line 78
Which turned out to be due to a new update in PHP 8 that doesn't allow non-array values to be used in the count function and throws a fatal error stopping the further script execution. For example, if you have a$_POST['checkboxes_checked']
and you docount($_POST['checkboxes_checked'])
then it will give the above error because by default it doesn't recognize it as an array. To fix this error, you can do:count((array)$_POST['checkboxes_checked']))
, which fixes the problem.
However, the problem in my case is that I have a couple of hundred files that need this problem fixed, I don't want to go inside each file and fix this as that would be extremely time-consuming. Is there a way to configure PHP 8 to ignore this and still proceed with the count function with these $_POST parameters? or some sort of search/replace regex that I can run on all files that replacecount($_POST['some_parameter_name'])
withcount((array)$_POST['some_parameter_name']))
? Honestly, I have no idea how I can fix this problem without manually going into each file, and this is the part where I need your help.
One temporary solution would be to define your owncount()
function. For instance:
/**
* @deprecated Temporary solution for count() TypeError on invalid countables
*/
function oldCount($input)
{
return isset($input) && is_array($input) ? count($input) : 0;
}
This is just a quick example. You can use your(array)
thing as well.
Now all you need to do is replace all the occurrences ofcount(
in your code byoldCount(
, and you're done.
WARNING: This is only meant as a temporary solution. It requires an extra function call every time it is used and PHP 8 has not incorporated a stricter check for nothing: It's there to protect you. Update your code as soon as possible.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.