Calculating VAT in PHP

Calculating VAT in PHP

Description

This function is a simple PHP VAT calculator. It will calculate the 17.5% UK sales VAT (Value Added Tax) on an amount and then round the price to two (2) decimal places. For example, £5.95 would be £6.99125 with VAT added, which would be rounded to £6.99. The amount of vat can be altered to any amount of sales tax, so we can calculate a new price for any string using any percentage. We can use this tax on a product in a shopping cart script.

Sample code

<?php

/*

We can calculate 17.5% VAT on a price of £4.67

$price_without_vat = 4.67

echo calculate_vat($price_without_vat);

This would return the new amount with 17.5% added, and would be rounded to 2 decimal places

*/

function calculate_vat($price_without_vat) {

$vat = 17.5; // define what % vat is

$price_with_vat = $price_without_vat + ($vat*($price_without_vat/100)); // work out the amount of vat

$price_with_vat = round($price, 2); // round to 2 decimal places

return $price_with_vat;

}

?>

 

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.