PHP K�sikirja
Edellinen | Luku 15. Operators | Seuraava

# Incrementing/Decrementing Operators

PHP supports C-style pre- and post-increment and decrement operators.

Taulu 15-5. Increment/decrement Operators

Example | Name | Effect
--- | --- | ---
++$a | Pre-increment | Increments $a by one, then returns $a.
$a++ | Post-increment | Returns $a, then increments $a by one.
--$a | Pre-decrement | Decrements $a by one, then returns $a.
$a-- | Post-decrement | Returns $a, then decrements $a by one.

Here's a simple example script:

`<?php
echo "<h3>Postincrement</h3>";
$a = 5;
echo "Should be 5: " . $a++ . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";
echo "<h3>Preincrement</h3>";
$a = 5;
echo "Should be 6: " . ++$a . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";
echo "<h3>Postdecrement</h3>";
$a = 5;
echo "Should be 5: " . $a-- . "<br />\n";
echo "Should be 4: " . $a . "<br />\n";
echo "<h3>Predecrement</h3>";
$a = 5;
echo "Should be 4: " . --$a . "<br />\n";
echo "Should be 4: " . $a . "<br />\n";
?>`

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in Perl 'Z'+1 turns into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ). Note that character variables can be incremented but not decremented.

Esimerkki 15-3. Arithmetic Operations on Character Variables

`<?php $i = 'W'; for($n=0; $n<6; $n++) echo ++$i . "\n";`

The above example will output:

    X
    Y
    Z
    AA
    AB
    AC

Incrementing or decrementing booleans has no effect.

Edellinen | Alkuun | Seuraava
Execution Operators | Ylös | Logical Operators