PHP allows you to increment strings. Why?
Let's jump ahead a bit. Take the following code:
$string = '9ZzZ';
echo ++$string; // 10AaA
From a purist point of view this may seem like nonsense, however, you can see there is clearly a pattern here. Each column is treated as a set, (numbers, uppercase letters, lowercase letters). Then each set behaves like its own numbering system.
The manual confirms this and states that they copied Perl:
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.
But, it doesn't actually explain why.
Perls manual states something very similar, but again goes in to no explanation as to why this extra magic was added.
The auto-increment operator has a little extra builtin magic to it. If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. If, however, the variable has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern /^[a-zA-Z][0-9]\z/ , the increment is done as a string, preserving each character within its range, with carry
Similarly enigmatically:
The auto-decrement operator is not magical.
Why is increment so important but decrement isn't? And why would they do this at all?
It's logical in its own way, and I can see uses for it (though you could write something more obvious in its place). I can even see why they didn't to mill through the ASCII character set (since that might not be the character set your using) or through the character set defined in php.ini as that could significantly alter behaviour between systems. So why bother at all?
3d8
when incremented becomes 4 (ideone.com/Yoon3l). Php doesn't have that restriction or the concept of a numbery thing vs a stringy thing. This means that the two types overlap to some o_O moments. ideone.com/ppfv3f