Fatal error: Uncaught TypeError: Unsupported operand types: string + float

PHP Errors No Comments

Fatal error: Uncaught TypeError: Unsupported operand types: string + float error comes when one of your variables is a string and another one is float.
Let’s say, we have the following code:
$Total = $Var1 + $Var2;

If this throws Fatal error: Uncaught TypeError: Unsupported operand types: string + float
error then simply add (float) before the variables and problem will be solved.

By doing this, we are just typecasting the variable to a float value. Hence the above line will look like:
$Total = (float)$Var1 + $Var2;
or
$Total = $Var1 + (float) $Var2;
or
$Total = (float)$Var1 + (float) $Var2;

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments