Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
This chapter is from the book

Incrementing and Decrementing a Number

PHP, like Perl and most other programming languages, includes some shortcuts that let you avoid ugly constructs such as

$tax = $tax + 1;

When you need to increase the value of a variable by 1 (called an incremental adjustment) or decrease the value of a variable by 1 (a decremental adjustment), you can use ++ or −−, respectively:

$var = 20; // 20
$var++; // 21
$var++; // 22
$var--; // 21

Solely for the sake of testing this concept, you’ll rewrite the handle_calc.php script one last time.

To increment the value of a variable:

  1. Open handle_calc.php in your text editor or IDE (Script 4.4).
  2. Change the tax rate calculation from Script 4.3 to read as follows (Script 4.5):

    $taxrate = $tax/100;
    $taxrate++;

    The first line calculates the tax rate as the $tax value divided by 100. The second line increments this value by 1 so that it can be multiplied by the total to determine the total with tax.

    Script 4.5. Incrementing or decrementing a number is a common operation using ++ or −−, respectively.

    1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
        1.0 Transitional//EN"
    2      "http://www.w3.org/TR/xhtml1/DTD/
           xhtml1-transitional.dtd">
    3   <html xmlns="http://www.w3.org/1999/
        xhtml" xml:lang="en" lang="en">
    4   <head>
    5      <meta http-equiv="Content-Type"
           content="text/html; charset=utf-8"/>
    6      <title>Product Cost Calculator</title>
    7      <style type="text/css" media="screen">
    8         .number { font-weight: bold;}
    9      </style>
    10  </head>
    11  <body>
    12  <?php // Script 4.5 - handle_calc.php #4
    13  /* This script takes values from
        calculator.html and performs
    14  total cost and monthly payment
        calculations. */
    15
    16  // Address error handling, if you want.
    17
    18  // Get the values from the $_POST array:
    19  $price = $_POST['price'];
    20  $quantity = $_POST['quantity'];
    21  $discount = $_POST['discount'];
    22  $tax = $_POST['tax'];
    23  $shipping = $_POST['shipping'];
    24  $payments = $_POST['payments'];
    25
    26  // Calculate the total:
    27  $total = (($price * $quantity) +
        $shipping) - $discount;
    28
    29  // Determine the tax rate:
    30  $taxrate = $tax/100;
    31  $taxrate++;
    32
    33  // Factor in the tax rate:
    34  $total = $total * $taxrate;
    35
    36  // Calculate the monthly payments:
    37  $monthly = $total / $payments;
    38
    39  // Apply the proper formatting.
    40  $total = number_format ($total, 2);
    41  $monthly = number_format ($monthly, 2);
    42
    43  // Print out the results:
    44  print "<div>You have selected to
        purchase:<br />
    45  <span class=\"number\">$quantity</span>
        widget(s) at <br />
    46  $<span class=\"number\">$price</span>
        price each plus a <br />
    47  $<span class=\"number\">$shipping</span>
        shipping cost and a <br />
    48  <span class=\"number\">$tax</span>
        percent tax rate.<br />
    49  After your $<span class=\"number\">
        $discount</span> discount, the total
        cost is
    50  $<span class=\"number\">$total
        </span>.<br />
    51  Divided over <span class=\"number\">
        $payments</span> monthly payments, that
        would be $<span class=\"number\">$monthly
        </span> each.</p></div>";
    52
    53  ?>
    54  </body>
    55  </html>
  3. Save the script, place it in the same directory as calculator.html, and test it in your browser (Figures 4.9 and 4.10).

    Figure 4.9

    Figure 4.9 The last execution of the form.

    Figure 4.10

    Figure 4.10 It won’t affect your calculations if you use the long or short version of incrementing a variable (compare Scripts 4.4 and 4.5).

  • + Share This
  • 🔖 Save To Your Account