Search the blog

If you’re programming an eCommerce site, sooner or later you’ll need to calculate VAT. Although it doesn’t change very often, this function allows you to set the VAT to anything you like. The $isVATInclusive is a boolean flag and determines whether the $value you pass already has VAT added or not. This is because sometimes you store prices inclusive of VAT and need to reverse calculate it. The value returned is the VAT only.

function vat($value, $vat, $isVATInclusive) {

    if($isVATInclusive === false) {

        $value = ($value * $vat) / 100;

    else {

        $percent        = 100 + $vat;
        $newValue       = $value / $percent;
        $newValue      *= 100;
        $value         -= $newValue;


    return number_format($value, 2);

Tim Bennett is a freelance web designer from Leeds. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.