# How To Calculate VAT GST In PHP – Simple Examples

Welcome to a quick tutorial on how to calculate VAT and GST in PHP. Working on your own eCommerce platform in PHP? Trying to figure out how tax should be calculated?

• To calculate the grand total with tax in PHP – `\$grand = \$total  * ((100 + TAX PERCENT) / 100);`
• To get the taxable amount – `\$tax = \$total * (TAX PERCENT / 100);`

That should cover the basics, but read on if you need more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in. ### QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

## PHP TAX CALCULATIONS All right, let us now get into the examples of how to calculate GST and VAT in PHP.

### 1) TAX AMOUNT FOR ALL ITEMS

1-items.php
``````<?php
// (A) DUMMY ITEMS (NAME => PRICE)
\$items = [
"Foo Bar" => 12.34,
"Goo Bar" => 8.76,
"Poo Bar" => 34.56
];

// (B) CALCULATE TOTALS + TAX
\$taxrate = 9; // TAX PERCENTAGE
\$subtotal = 0;
\$tax = 0;
foreach (\$items as \$name=>\$price) {
\$itemtax = \$price * (\$taxrate / 100);
\$subtotal += \$price;
\$tax += \$itemtax;
echo "\$name (Price) - \$price<br>";
echo "\$name (Taxable) - \$itemtax<br>";
}

// (C) GRAND TOTAL
\$grand = \$subtotal + \$tax;
echo "Subtotal - \$subtotal<br>";
echo "Grand Total - \$grand<br>";``````

Yep, this example is rather intimidating at first, but keep calm and look carefully.

• We are basically looping through the list of items.
• Calculating the taxable amount for each item.
• Then add them together to get the sub-total, total taxable, and grand total.

### 2) CALCULATE TOTAL TAXABLE

2-total.php
``````// (B) CALCULATE TOTALS + TAX
\$taxrate = 9; // TAX PERCENTAGE
\$subtotal = 0;
\$tax = 0;
foreach (\$items as \$name=>\$price) {
\$subtotal += \$price;
}

// (C) GRAND TOTAL
\$tax = \$subtotal * (\$taxrate / 100);
\$grand = \$subtotal + \$tax;
echo "Subtotal - \$subtotal<br>";
echo "Grand Total - \$grand<br>";``````

If you don’t need to calculate the taxable amount for each item, this is the faster way – Simply add the prices of all the items to get the sub-total, then calculate the tax and grand total.

### 3) ALTERNATE WAY TO CALCULATE TAX

3-alternate.php
``````// (C) GRAND TOTAL
\$grand = \$subtotal * ((100 + \$taxrate) / 100);
\$tax = \$grand - \$subtotal;
echo "Subtotal - \$subtotal<br>";
echo "Grand Total - \$grand<br>";``````

Lastly, this is another way to calculate the tax – We directly calculate the grand total and work backward to get the taxable amount. That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

### EXTRA) ROUNDING OFF

``````\$price = 123.456789;

// (A) ROUND OFF TO 2 DECIMAL PLACES
\$rounded = round(\$price, 2); // 123.46

// (B) CEILING - ALWAYS ROUND UP TO NEXT WHOLE NUMBER
\$ceiling = ceil(\$price); // 124

// (C) FLOOR - ALWAYS ROUND DOWN TO LAST WHOLE NUMBER
\$ceiling = ceil(\$price); // 123``````

Yep, the native PHP rounding functions should be enough in most cases. But take note that some companies or regions may have very specific rounding rules. For example, I once worked with a POS that rounded off to the nearest 5 cents:

• 0.01 to 0.24- Round down to 0.
• 0.25 to 0.49 – Round up to 0.5.
• 0.51 to 0.74 – Round down to 0.5.
• 0.75 to 0.99 – Round up to 1.

So ask your client if you are not sure. Consulting an accountant is the best way to get the rounding right. 