Fatal error: Uncaught TypeError: Unsupported operand types: string * string in /Users/khushbuoswal/Desktop/Grocery_Store/cart.php:212 Stack trace: #0 {main} thrown in /Users/khushbuoswal/Desktop/Grocery_Store/cart.php on line 212
<td>
<span>$</span>
<span class="product-price"><?php echo $value['product_quantity'] * $value['product_price']; ?></span>
</td>
I have a price in decimal in SQL database
I have tried putting intaval. I want this error to get resolved.
2
Answers
You need to convert these values to numbers before multiplying them. The
floatval
function can convert the strings to floating-point numbers, which can handle decimal values.This error happens when PHP can’t automatically convert a string to a number in order to do the mathematical operation you asked for.
For instance,
'5' * '1.5'
will give7.5
, but'apples' * 'oranges'
will give the error you’re seeing.That means either
$value['product_quantity']
or$value['product_price']
(or both) doesn’t contain anything that looks like a number. Possibly they’re empty strings, or possibly they’ve been formatted wrong, e.g. the price is'€42.00'
instead of just'42.00'
.You need to debug the code to find out what value they have, and why it wasn’t what you expected. Do not just force the value to a float – it will make the error go away, but only by forcing all invalid values to zero, which is likely to cause much worse problems.