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

Home > Articles

Using Numbers

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

Incrementing and Decrementing a Number

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

$tax = $tax + 1;

When you need to increase the value of a variable by 1 (known as an incremental adjustment) or decrease the value of a variable by 1 (a decremental adjustment), you can use ++ and --, 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, if it is not already open (Script 4.4).

  2. Change the tax rate calculation from Script 4.3 to read as follows (Script 4.5):

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

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

    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.3 - handle_calc.php #4
    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) +
        $shipping) - $discount;
    27
    28  // Determine the tax rate:
    29  $taxrate = $tax / 100;
    30  $taxrate++;
    31
    32  // Factor in the tax rate:
    33  $total = $total * $taxrate;
    34
    35  // Calculate the monthly payments:
    36  $monthly = $total / $payments;
    37
    38  // Apply the proper formatting:
    39  $total = number_format ($total, 2);
    40  $monthly = number_format ($monthly, 2);
    41
    42  // Print out the results:
    43  print "<p>You have selected to
        purchase:<br>
    44  <span class=\"number\">$quantity</span>
        widget(s) at <br>
    45  $<span class=\"number\">$price</span>
        price each plus a <br>
    46  $<span class=\"number\">$shipping</span>
        shipping cost and a <br>
    47  <span class=\"number\">$tax</span>
        percent tax rate.<br>
    48  After your $<span
        class=\"number\">$discount</span>
        discount, the total cost is
    49  $<span class=\"number\">$total</
        span>.<br>
    50  Divided over <span
        class=\"number\">$payments</span>
        monthly payments, that would be $<span
        class=\"number\">$monthly</span> each.</
        p>";
    51
    52  ?>
    53  </body>
    54  </html>

    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.

  3. Save the script, place it in the same directory as calculator.html, and test it in your browser a.jpg b.jpg.

04fig10.jpg

Click to view larger image

a.jpg The last execution of the form.

04fig11.jpg

Click to view larger image

b.jpg 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