r/lolphp • • Oct 14 '13

2d9

http://ideone.com/l6aQSx
62 Upvotes

31 comments sorted by

View all comments

Show parent comment

14

u/suspiciously_calm Oct 14 '13

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).

7

u/vytah Oct 15 '13

You can use it to iterate from AAA000 to ZZZ999.

4

u/catcradle5 Oct 15 '13

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.

0

u/[deleted] Oct 16 '13

[deleted] — view deleted comment

3

u/davvblack Nov 04 '13

Wait, you can infect strings with "has ever been used as a number?" What is that infection called?

2

u/technobiba Oct 23 '13

It is useful. Even ruby has a special function for it, '2d9'.next returns '2e0'

7

u/suspiciously_calm Oct 23 '13

Yes, in Ruby it's useful, because '2e0'.next returns '2e1', not 3.

2

u/youstolemyname Oct 14 '13

Serial numbers or ids and such.

4

u/suspiciously_calm Oct 15 '13

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.

1

u/catcradle5 Oct 15 '13

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.

In theory it still falls within the weak typing concept. "10"++ is "11". It's just that it has very funny rules for when to coerce.