Incrementing and Decrementing a Number

I l @ ve RuBoard

PHP, like Perl and most other programming languages, includes some shortcuts so that you may avoid ugly constructs such as $Tax = $Tax + 1;. When you are in a situation where you need to increase the value of a variable by just one (called an "incremental" adjustment) or decrease the value of a variable by just one (a "decremental" adjustment) you can use "++" or "" accordingly .

To increment the value of a variable:

  1. Open numbers .php in your text editor.

  2. Change line 10 of Script 4.3 to read $Tax++;

    Script 4.3. Your script is now marginally cleaner than it was before since you used the "++" incrementing method. The mathematical result will be the same regardless.

    graphics/04sc03.jpg

  3. Save your script, upload it to the server, and test it in your browser (Figure 4.4).

    Figure 4.4. It will not affect your calculations if you use the long or short version of incrementing a variable (compare Scripts 4.2 and 4.3).

    graphics/04fig04.gif

Tip

Although functionally it will not matter whether you code $Tax = $Tax + 1; or the abbreviated $Tax++, the latter (using the increment operator) is the more professional and common method.


Tip

In Chapter 6, Conditional Statements and Loops, you'll see how the increment operator is commonly used to count through a series of actions.


Tip

To decrement a variable, you merely use the subtraction symbol (-) twice instead of the plus sign, like this:

 $Number; 

I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net