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;

5 4 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments