1

I am having an error. A non well formed numeric value encountered. Kindly check my code below and guide me what I am doing wrong:

...
    $i++;
    $item_count += $item['quantity'];
    $sub_total += ($product['price'] * $item['quantity']);
    $sub_total = (float)$sub_total;

}
$tax_rate = 18;

$tax = ($tax_rate * $sub_total) / 100;
// Here I am having an error - A non well formed numeric value encountered
$tax = number_format($tax, 2); // 2 decimal digit
$grand_total = $tax + $sub_total;

Kindly suggest what I am doing wrong. Your suggestions would be welcome. Thank You

Jsowa
  • 9,104
  • 5
  • 56
  • 60
Sarah
  • 405
  • 2
  • 7
  • 23

1 Answers1

1

number_format is function used for formatting strings. You may and should use it to view values, not to calculate them. You may change lines:

$grand_total = $tax + $sub_total;
$tax = number_format($tax,2);

But it would be a bit safer to use another variable:

$view_tax = number_format($tax,2);
$grand_total = $tax + $sub_total;
// now $grand_total uses $tax and to view
// tax, just use $view_tax variable
Łukasz
  • 89
  • 8
  • Give the user a chance to delete the question if user feels like by answering what could have been solved with comments the user can no longer delete the question as users problem has been solve in comments answer was unnecessary. – Bobby Axe Aug 27 '18 at 01:16