PHP Incrementing and Decrementing Operators

This shorthand syntax is a nice and simple way to increase or decrease an integer variable by 1:

$i = 17;
$i++;
echo $i; // prints "18"

And for subtracting:

$i = 88;
$i--;
echo $i; // prints "87"

You may know this already, but there is a bit more to these operators when working with strings that you might not be aware of:

$s = 'A';
$s++;
echo $s; // Output: B

It gets even more interesting…

$s = 'E1';
$s++;
echo $s; // Output: E2

$s = 'E9';
$s++;
echo $s; // Output: D1

$s = 'E09';
$s++;
echo $s; // Output: E10

See more examples in the PHP Manual page.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *