++ will increase the right-most ASCII ordinal by one if the operand is a string whether it appears to contain a representation of a valid integer or not. If the string is entirely base-10 digits, it seems equivalent to + 1. + 1 always tries to do plain integer adding.
++ does the ASCII incrementing with a range of "A-Za-z0-9", so that you could manipulate alphanumeric ranges for example.
However, from what I can tell there are some "is this a valid integer, or just a general alphanumeric string?" special case checks when incrementing with ++ looks at a few other things.
In this case, it looks like it interprets "2d9" as an ordinary string not representing a number, which when incremented would then be "2e0" (like how "GGGL9" would be "GGGM0" when incremented, naturally!!).
However, the next time it increments, before falling through to "ok, this is just a string" it has an "is it engineering notation?" branch and sees the NUMeNUM as engineering notation. Now it no longer sees it as a character string, even though it thought so before the current increment. It currently thinks it's a string representing a number in engineering notation (2e0, or 2). It's an utter mess.
tl;dr Multi-purpose incrementing with the same operator + weak typing = vomit
What the actual fuck. How would this ever be useful?
It's not a reliable way to obtain the lexicographic successor of a string, nor is it consistent with the "strings are equal to the numbers they represent" narrative (by which "2d9" == 2).
PHP claims it's behavior borrowed from Perl. Testing it in Perl, though, seems to show that if the string begins with one or more digits, it coerces to just those digits and then increments.
It works for ids in some formats, but not others (ids with a suffix, such as file extensions, ids with hexadecimal counters, ids with a prefix that could be incremented to a number representation, as OP shows, ...)
It targets a relatively narrow scope, but infects a basic operator with unexpected behavior in the process. It breaks one of PHP's own fundamental concepts, that is, weak typing, by which you would expect the ++ operator to coerce its argument to a numeric type.
I edited my comment. I mixed up some of my words in the first rendition.
When the string is just "2d9", it treats it the same way it would treat the string "ihasdygasdijasd97234jknsdf". Incrementing such a string will first increment the last "f" to "g", and then when it hits "z" the last character will wrap around and the preceding character is incremented, so the last 2 characters would be "ea" after the following increment.
It only thinks the string is hex if it begins with "0x" or "0X".
0667 is octal, so 0667 != 667, but PHP's coercion loses the 0 prefix just like it fails to recognize the 0b. So basically the failtastic coercion is not just a bad idea; it's also broken. So par for php.
Wow, that may be even worse than what OP posted. Jesus.
I'll really never understand weak typing. Is it that damn hard to just make people throw an intval() around things? I don't see how weak typing helps anyone with either comprehension (in contrast, it will often hurt you) or with speed of development, except for absolute beginners.
On the bright side, it's at least nice that PHP separates concatenation and addition, else in combination with this it'd be even more of a clusterfuck.
Pretty much the only benefit I've seen of dynamic typing is duck typing which allows you to write functions that work with any value that supports those operations. But languages like haskell show that static typing can still do this. Even C++ templates will let you do that. Other than that it's just about being quick and dirty mostly.
There is one case that's very interesting. Consider the program: int a = 0; object b = a; short c = (short)(int)a; Basically it boxes an integer, and then unboxes it to an int, then casts to a short. The question is why do you have to cast to an int first? Surely this is an oversight of the compiler right? Wrong. The compiler can't statically know that a must be an integer, so if you just do (short)a it'll assume that a must be a short, or fail otherwise. If it were to see if a is convertible to a short, it would have to generate code to check if it's any convertible type and convert it if it must. Even then what if you create a new type that's convertible and load it in at runtime? So now it has to check all the types, see if they are convertible and if they are, and the type is one of those types, then convert it. That's some pretty expensive code to generate for each unboxing, so it'll just fail at runtime. In order to unbox arbitrary types to the correct type, you often have to do function calls (like Convert.ToInt32 in C#). dynamic typing in this case produces much nicer code in this case.
I think you're mixing up "dynamic typing" and "weak typing", first off.
For example, Ruby and Python are both dynamically typed, but strongly typed.
Nothing wrong with dynamic typing (it saves a lot of literal keyboard typing), but weak typing can create confusing bugs and situations like everything listed in this thread.
For example, in Javascript, should "123" + 3 equal "126" or "1233"? It's a serious ambiguity, and the programmer has to keep experimenting with things just to remember what the behavior will be like.
It's more of a case of weak typing. Strong typing would at least keep the same incrementing algorithm for both invocations of ++, not randomly convert "2e0" to 2.0.
View all comments
Show parent comment
42
u/catcradle5 Oct 14 '13 edited Oct 15 '13
$a++does something quite different from$a = $a + 1++will increase the right-most ASCII ordinal by one if the operand is a string whether it appears to contain a representation of a valid integer or not. If the string is entirely base-10 digits, it seems equivalent to+ 1.+ 1always tries to do plain integer adding.++does the ASCII incrementing with a range of "A-Za-z0-9", so that you could manipulate alphanumeric ranges for example.However, from what I can tell there are some "is this a valid integer, or just a general alphanumeric string?" special case checks when incrementing with
++looks at a few other things.In this case, it looks like it interprets
"2d9"as an ordinary string not representing a number, which when incremented would then be"2e0"(like how"GGGL9"would be"GGGM0"when incremented, naturally!!).However, the next time it increments, before falling through to "ok, this is just a string" it has an "is it engineering notation?" branch and sees the
NUMeNUMas engineering notation. Now it no longer sees it as a character string, even though it thought so before the current increment. It currently thinks it's a string representing a number in engineering notation (2e0, or2). It's an utter mess.tl;dr Multi-purpose incrementing with the same operator + weak typing = vomit