Taken from the documentation, PHP follows Perl's convention when dealing with arithmetic operations on character variables:
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 characters (a-z and A-Z) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.
While I personally have not used this feature, I can think of a few benefits by following Perl's convention, as opposed to C's. That is incrementing within the alpha range as opposed to simply incrementing the ASCII value.
By staying within the alpha range, essentially we're talking about a base 26 numeral system (using letters). So just as 9
increments to 10
(carry over), z
increments to aa
.
What comes to mind immediately would be character hash sequences.
public static function nextSeed() {
// $seed = abc
return strtolower(++self::$seed);
// $seed = abd
}
While likely such seeds would be managed outside of PHP, it is nonetheless beneficial PHP provides natively what would otherwise require more code.
Other use cases:
- Base-26 Numeral System Representation
- Calculating character differences
- Encryption