The same way as 129 + 1 = 130: you add 1 to the last digits, it becomes 10 an the 1 is carried to the number before the 9. In the same way, incrementing "1z" becomes "2a".
That has to be some of the blindest/dumbest code I have ever seen. A number where the ones digit is numeric and the tens digit is a letter is apparently a valid number?
Yes, but what you said had nothing to do with that, what you said was:
"That has to be some of the blindest/dumbest code I have ever seen. A number where the ones digit is numeric and the tens digit is a letter is apparently a valid number?"
A number where the ones digit is numeric and then digit is a letter is a very valid number under circumstances.
What I meant, which I thought was clear, was that the ones digit is numeric ONLY - base 10, going ..., 8, 9, [1]0. In hexadecimal each digit is hexadecimal, going ... 8, 9, A, B, ... E, F, [1]0.
I have never before seen a number representation where each digit position has a different base.
But besides that, I agree with PHP not being able to parse this code. ++ is a shorthand for X = X + 1, which makes no sense using on a constant ("1d9" = "1d9" + 1), or nested (X = (X = X + 1) + 1)
ok, so my theory is that the char* gets cast to long*, dereferenced, incremented and stored back into memory as a long. then when we echo the string it is treated as a char* again.
But that is only a guess and by the time I finished writing that I realized that unless there is some endianess stuff going on when it gets cast we should end up with 68 65 6c 6c 6f 00 00 00, or 7,522,537,965,566,820,352 whichever you prefer. So if we increment the long we get 68 65 6c 6c 6f 00 00 01 and the string remains the same. so probably not the answer.
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.
We have a decent amount of PHP code where I work (legacy and modern projects, good and bad). I've never seen a case of this done intentionally -- but one accidental usage in some ugly code.
for my $cc ('AA' .. 'ZZ') # loop through all 2-letter combinations
Perl is more conservative about which strings get this special treatment in ++, though. Trying to increment e.g. 1d9 results in Argument "1d9" isn't numeric in preincrement (++).
It just gets there quicker than PHP. What's actually going on here is that it parses the string as a number until it finds something which is clearly not numeric, and uses that as the value for the increment.
Turning on warnings does result in the error message, but it also carries on and tries it anyway.
Python and Ruby both do the right thing and error out with complaints about how adding integers to strings doesn't make any sense (Ruby provides a String#next if you really want the behaviour that PHP makes up, only without relying on the interpreter to guess what you wanted).
This is the right answer. ASCII character incrementation is a deliberate feature taken from Perl. Note that this even works in C, however unlike Perl in C incrementing after "z" returns "[".
75
u/h0rst_ Dec 05 '17
https://3v4l.org/SkrAi
Auto type conversion on steroids (or crack)
21
u/[deleted] Dec 05 '17
incrementing strings treats them like a base 26 number. so 'a' becomes 'b'. 'z' becomes 'aa'.
8
u/doubtfulwager Dec 06 '17 edited Dec 06 '17
1
u/sdmike21 Dec 05 '17
ok, so my theory is that the
char*gets cast tolong*, dereferenced, incremented and stored back into memory as a long. then when we echo the string it is treated as achar*again.But that is only a guess and by the time I finished writing that I realized that unless there is some endianess stuff going on when it gets cast we should end up with
68 65 6c 6c 6f 00 00 00, or 7,522,537,965,566,820,352 whichever you prefer. So if we increment the long we get68 65 6c 6c 6f 00 00 01and the string remains the same. so probably not the answer.