Flylib.com

Books Software

 
 
 

Incrementing and Decrementing


Incrementing and Decrementing

A common thing to do in PHP scripts, especially in the loops we'll see later in this chapter, is to increment the value in a variable by adding one or to decrement the value in a variable by subtracting one. In PHP, the incrementing operator is ++ , and the decrementing operator is -- .

For example, if $bananas holds , then using the ++ operator like this: $bananas++ leaves a result of 1 in $bananas . If $apples holds 11 , then $apples-- leaves 10 in $apples .

You can use ++ and -- before or after a variable name , such as ++$bananas (this is using it as a prefix operator ) or $bananas++ (this is using it as a postfix operator ). If you use ++$bananas , the value in $bananas is incremented, and that incremented value is then used in the rest of the statement. If you use $bananas++ , then the original value in $bananas is used in the rest of the statement, and then the value in $bananas is incremented. Here's an overview of the difference:

++$value

Pre-increment

Increments $value by one, then returns $value .

$value++

Post-increment

Returns $value , then increments $value by one.

--$value

Pre-decrement

Decrements $value by one, then returns $value .

$value--

Post-decrement

Returns $value , then decrements $value by one.


In other words, if you use ++$bananas , the value in $bananas is incremented before that value is passed on for use in the rest of the current statement. If you use $bananas++ , the current value in $bananas is passed on for use in the rest of the statement, and only then is the value incremented.

You can see this difference in Example 2-2, phpincrementing.php, which works through the various possibilities, using ++ and -- as both a prefix and postfix operator.

Example 2-2. Incrementing and Decrementing, phpincrementing.php
<HTML>
    <HEAD>
       <TITLE>
           Incrementing and Decrementing
       </TITLE>
    </HEAD>

    <BODY>
        <H1>
            Incrementing and Decrementing
        </H1>
<?php
    $a = $b = $c = $d = 1;

    echo "$a++ gives ", $a++, "<BR>";

    echo "++$b gives ", ++$b, "<BR>";

    echo "$c- - gives ", $c--, "<BR>";

    echo "- -$d gives ", --$d, "<BR>";
?>
    </BODY>
</HTML>

The results appear in Figure 2-2. As you can see, it makes a difference whether you use ++ and -- as prefix operators or postfix operators.

Figure 2-2. Using the increment and decrement operators.


If you just want to make sure you increment or decrement the value in a variable without worrying about whether the value will be incremented or decremented before being used in the rest of the statement, use ++ and -- as prefix operators.


Operator Precedence

Now that we've got all these new operators floating around + , - , ++ , % , and so ona new question arises: if you use a bunch of these operators at the same time, which comes first? For example, what's the result of the following script?

<?php
    echo 4 + 2 * 9;
?>

Will the + part be evaluated first, giving 4 + 2 * 9 = 6 * 9 = 54 , or will the * part be evaluated first, giving 4 + 2 * 9 = 4 + 18 = 22 ?

The answer is 22 because the * operator has precedence over the + operator when you mix them up like this. You can see the complete list of PHP operator precedence in Table 2-1. The operators with the highest precedence are at the top, and the lowest are at the bottom. As you can see in the table, the * operator is above the + operator.

If operator precedence is interfering with your goal, you can always clarify how you want items to be evaluated by using parentheses. For example, you'll get 22 when you run this script:

<?php
    echo 4 + 2 * 9;
?>

But you can use parentheses to tell PHP what to do. If you change this expression to (4 + 2) * 9 , then you'll get 6 * 9 = 54 . Take a look at phpprecedence.php in Example 2-3 to see this at work.

Example 2-3. Incrementing and Decrementing, phpprecedence.php
<HTML>
    <HEAD><TITLE>Setting operator precedence</TITLE></HEAD>
    <BODY>
        <H1>Setting operator precedence</H1>
<?php
    echo "4 + 2 * 9 = ", 4 + 2 * 9, "<BR>";
    echo "(4 + 2) * 9", (4 + 2) * 9, "<BR>";
    echo "4 + (2 * 9)", 4 + (2 * 9), "<BR>";
?>
    </BODY>
</HTML>

The results, where we've set our own execution precedence, appear in Figure 2-3.

Figure 2-3. Setting execution precedence.