Expecting an Int/Float but says it has a String

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.

    static function getMinuteComponent($time){
    	if($time>0){
    		$minutes = round(($time - floor($time))*60);
    		return $minutes;
    	}
    	$minutes = 00;
    	return $minutes;
    }

no CakePHP specific problem here, thats a pure PHP problem.

I am not going to just provide the solution for you since it should be pretty easy for you to solve yourself.

I just want to ask you: are you sure you pass in actual integer/float values into that function or could they may be something else?

Also just so you know: Official PHP 8.1+ support was added in CakePHP 4.3.0
So don’t expect your ancient CakePHP 3.3 to work flawlessly with PHP 8.1

I am not as smart as you so let me ask two questions.

  1. 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?
  2. 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.

Would you still hold to the view that it is not a Cake issue but a simple PHP problem? If so please educate me.

you need to use gettype to see the actual type, not by just using echo to show the value. Try :

echo gettype($time);

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.)

2 Likes

@alendon , try checking out declare(strict_types=1); It is useful to catch mismatch types and errors.

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.

Going back to PHP 7.1 is not an option.

I decided to roll back PHP to 7.1

Everybody lies.
– Dr. House