PHP supports C-style pre- and post-increment and decrement operators.
Note: The increment/decrement operators only affect numbers and strings. Arrays, objects and resources are not affected. Decrementing
NULLvalues has no effect too, but incrementing them results in 1.
|++$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:
$a = 5;
echo "Should be 5: " . $a++ . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";
PHP follows Perl’s convention when dealing with arithmetic operations on character variables and not C’s. For example, in PHP and Perl $a = ‘Z’; $a++; turns $a into ‘AA’, while in C a = ‘Z’; a++; turns a into ‘[‘ (ASCII value of ‘Z’ is 90, ASCII value of ‘[‘ is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.