PHP

HOW TO CALCULATE GST/VAT IN PHP

EACH ITEM THEN ADD

01

DUMMY ITEMS (NAME => PRICE) $items = ["Foo" => 12.34, "Goo" => 8.76, "Poo" => 34.56];

CALCULATE ALL ITEMS $taxrate = 9; $subtotal = 0; $tax = 0; foreach ($items as $name=>$price) {   $itemtax = $price * ($taxrate / 100);   $subtotal += $price;   $tax += $itemtax; }

GRAND TOTAL $grand = $subtotal + $tax;

ALL ITEMS AT ONCE

02

DUMMY ITEMS (NAME => PRICE) $items = ["Foo" => 12.34, "Goo" => 8.76, "Poo" => 34.56];

SUBTOTAL FOR ALL ITEMS $taxrate = 9; $subtotal = 0; $tax = 0; foreach ($items as $name=>$price) { $subtotal += $price; }

TAX & GRAND TOTAL $tax = $subtotal * ($taxrate / 100); $grand = $subtotal + $tax;