skip to Main Content

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


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

    <td>
       <span>$</span>
       <span class="product-price"><?php echo floatval($value['product_quantity']) * floatval($value['product_price']); ?></span>
    </td>
    
    Login or Signup to reply.
  2. 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 give 7.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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search