PHP for the World Wide Web (Visual QuickStart Guide)

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.

  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).

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

Категории