PHP Strings – Incrementing Strings in PHP and Perl

operatorsperlPHPstrings

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?

Best Answer

My understanding is that the feature was originally intended for generating sequences of filenames for scripts that need to produce variable numbers of output files. While you could use a simple counter for the purpose, this hasa few drawbacks (unpredictable name length causes bad sorting when files are listed alphabetically; letters are preferable to digits when many files are needed in order to keep generated names short), both of which can be solved by initialising your counter to (for example) 'aaaa'.

While other solutions are clearly possible and arenot even especially hard, perl's solution is remarkably concise.

Why php copied the idea, I'm not sure. It was never really intended for the type of application that benefits from this. It perhaps has something to do with the fact that perl was the dominant language for web scripting at the time.

Related Topic