For one item, multiply its unit price by its quantity: $subtotal = $unitPrice * $quantity;. For an order with multiple items, calculate each line amount and add them. Keep shipping, discounts, tax, and the final total as separate calculations so each amount has a clear meaning.
Calculate a subtotal for one item
PHP uses the * operator for multiplication. If $unitPrice holds the price of one unit and $quantity holds the number purchased, multiply them to get that line’s subtotal:
$unitPrice = 12.50;
$quantity = 3;
$subtotal = $unitPrice * $quantity;
// $subtotal is 37.5
The result is the amount for those units before any separately calculated shipping, discount, or tax. PHP’s + and - operators add and subtract amounts. See the PHP manual’s arithmetic-operator reference.
Add line subtotals for multiple items
Calculate each product’s line amount, then add the amounts to get the merchandise subtotal:
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11#1 Best Overall
$shirtPrice = 20.00;
$shirtQuantity = 2;
$mugPrice = 8.50;
$mugQuantity = 3;
$shirtsSubtotal = $shirtPrice * $shirtQuantity;
$mugsSubtotal = $mugPrice * $mugQuantity;
$subtotal = $shirtsSubtotal + $mugsSubtotal;
// $subtotal is 65.5
Using separate names makes it easier to inspect each line and verify the aggregate amount.
Keep order adjustments distinct
A subtotal is not necessarily the same as the final amount due. One worked order-total example multiplies price by quantity, adds shipping, subtracts a discount, and then applies a percentage tax. That is one example sequence, not a universal rule for tax or pricing. The correct order and treatment depend on the application’s pricing policy and applicable tax rules. Pearson’s sample for PHP for the Web: Visual QuickStart Guide shows an example of this kind of sequence.
Rank #2
Keep each stage separately named rather than folding every adjustment into $subtotal:
$subtotal = $shirtsSubtotal + $mugsSubtotal;
$discount = /* calculate according to your pricing rules */;
$shipping = /* calculate according to your shipping rules */;
$tax = /* calculate according to applicable tax rules */;
$total = $subtotal - $discount + $shipping + $tax;
This illustrative expression assumes those particular adjustments apply in that order; it is not a recommendation for every store or jurisdiction.
Recommended Free Tools
Use an intentional money and rounding approach
PHP arithmetic operators are sufficient to demonstrate the calculation, but production billing needs a defined currency, precision, and rounding policy. Formatting a value to two decimal places affects its display; it does not decide how intermediate amounts are rounded.
PHP’s division operator can return a floating-point value, except in the documented case where both operands are integers (or numeric strings coerced to integers) and the numerator is divisible by the divisor. See the PHP manual for the operator’s behavior.
Rank #4
For more involved monetary calculations, libraries can make currency and rounding choices explicit:
- MoneyPHP documents immutable money operations and rounding modes including half-up, half-even, up, and down, as well as rounding to a currency unit.
- Brick Money provides currency-aware money values; its documentation notes that adding values in unlike currencies raises an exception.
Choose the rounding rule deliberately, particularly when calculations involve taxes or more than one currency.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

