php calculate price code example

Example: how to calculate quantity and price in php

1   <!doctype html>
2   <html lang="en">
3   <head>
4      <meta charset="utf-8">
5      <title>Product Cost Calculator</
    title>
6      <style type="text/css">
7         .number { font-weight: bold; }
8      </style>
9   </head>
10  <body>
11  <?php // Script 4.2 - handle_calc.php
12  /* This script takes values from
    calculator.html and performs
13  total cost and monthly payment
    calculations. */
14
15  // Address error handling, if you want.
16
17  // Get the values from the $_POST array:
18  $price = $_POST['price'];
19  $quantity = $_POST['quantity'];
20  $discount = $_POST['discount'];
21  $tax = $_POST['tax'];
22  $shipping = $_POST['shipping'];
23  $payments = $_POST['payments'];
24
25  // Calculate the total:
26  $total = $price * $quantity;
27  $total = $total + $shipping;
28  $total = $total - $discount;
29
30  // Determine the tax rate:
31  $taxrate = $tax / 100;
32  $taxrate = $taxrate + 1;
33
34  // Factor in the tax rate:
35  $total = $total * $taxrate;
36
37  // Calculate the monthly payments:
38  $monthly = $total / $payments;
39
40  // Print out the results:
41  print "<p>You have selected to
    purchase:<br>
42  <span class=\"number\">$quantity</
    span> widget(s) at <br>
43  $<span class=\"number\">$price</span>
    price each plus a <br>
44  $<span class=\"number\">$shipping</
    span> shipping cost and a <br>
45  <span class=\"number\">$tax</span>
    percent tax rate.<br>
46  After your $<span
    class=\"number\">$discount</span>
    discount, the total cost is
47  $<span class=\"number\">$total</
    span>.<br>
48  Divided over <span
    class=\"number\">$payments</span>
    monthly payments, that would be
    $<span class=\"number\">$monthly</
    span> each.</p>";
49
50  ?>
51  </body>
52  </html>

Tags:

Php Example