I have just updated to PHP8.1 on a hosted site for work and running an application built in CakePHP 3.3 that I did not code. Prior to PHP update (from 7.1) the Cake app worked fine. Now I have the erorr
floor(): Argument #1 ($num) must be of type int|float, string given
I am a CakePHP newbie and am not sure how to resolve this. Can someone please advise me. Going back to PHP 7.1 is not an option. Code containing the error is below.
I am not as smart as you so let me ask two questions.
No code has changed in the app, but moving from PHP 7.4 to 8.1 has caused this issue. Why couldn’t it potentially be a cake issues esp when you say 8.1 support was not added until 4.3.0?
If $time is of the wrong type, how did the if statement evaluate correctly to then have an issue with the floor function.
Also, I only showed you one function (getMinuteComponent). There is a getHourComponent that also uses $time but it has not generated an error.
I decided to roll back PHP to 7.1. I slightly modifed my function to have an output to screen. See below.
static function getMinuteComponent($time){
echo $time.“ \n”;
if($time>0){
$minutes = round(($time - floor($time))*60);
return $minutes;
}
$minutes = 00;
return $minutes;
}
As expected. No errors. The page renders and functions as epected. As you can see from the screen shot, $time is float.
PHP has historically been very lenient about typing, and would happily convert a string into an int for you, if you used it in a context that required an int. This has also historically led to many, many bugs, so they are tightening that up in PHP8, and things which were not errors before will now be reported as errors.
Thanks yousuo and Zuluru, I have tried this and yes it does reveal $time to be a string.
I am relatively new to PHP & Cake and am learning via another person’s project I need to fix.
I really apprecate the feedback that I can build on as opposed to being told it is a simple PHP problem.
Good opportunity for a meta-learning experience here. When you ask experts, and they tell you something, but that thing conflicts with your own assumptions, a good starting point is often to question your assumptions, not the experts. (Not that the experts are always right, but at least if you question your assumptions and find them to be true, that’s extra information you can provide to others. And, I often find that, in trying to cover all my bases in what I provide to those I’m asking for help, I find the solution for myself.)
Sorry to say, most people responding in cakephp forums are pros (excluding me). The good thing is you get really good answers on complex issues. It may be a bit hard to understand some items.