Given the price, in pounds, of an item, any discount allowed and the applicable VAT rate, find and output the cost of an item.
<? // the current uk vat rate define(VAT_RATE, 17.5); // the price in pounds of an item // produces a result between 1 and 10 $price = mt_rand(1, 20); // the discount for this item (percent) // produces a result between 1 and 20 $discount = mt_rand(1, 20); // show the original price // number format is used to make sure the price has two decimal places echo 'Original price: £' . number_format($price, 2) . '<br /> <br />'; // work out the discount $item_discount = $price * ($discount / 100); // the cost would be the original price minus the discount $price -= $item_discount; // show the discount echo $discount . '% discount: £' . number_format($item_discount, 2) . '<br />'; // show the price after discount echo 'After discount: £' . number_format($price, 2) . '<br /> <br />'; // work out the vat $item_vat = $price * (VAT_RATE / 100); // plus vat $price += $item_vat; // show the vat echo VAT_RATE . '% VAT: £' . number_format($item_vat, 2) . '<br />'; // show the price after vat has been added echo 'After VAT: £' . number_format($price, 2);?>Which produces:
Original price: £12.00
15% discount: £1.80
After discount: £10.20
17.5% VAT: £1.79
After VAT: £11.99