Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It seems to me that the issue is that $i++ is nonsensical if $i is a string (and $i is a string, not a character). But instead of raising an error, PHP soldiers on and tries to apply some completely unexpected function.


It's actually occasionally useful if you're trying to gensym a string. In a normal language, you need to write your own gensym function - in PHP, you know that $s++ will create a sequence of unique strings, suitable for use in b64-only environments, that will never run out.


Perl does this, too. And is it really that unexpected? How many possible things could 'z'+1 return? The only possibilities that make sense to me are "{", "aa", or an error. I like PHP because it's flexible, and this often makes life easier. If you're really going to try to use strings as integers, then you deserve what's coming to you. If you're going to use this behavior as a feature, then you've just saved yourself a chunk of time.


>How many possible things could 'z'+1 return?

That are less than or equal to z?


To z or to 'z'?


Yes.


More sensible would be to not overload the ++ or +1 operator to do this but provide a different function. Then the capability is there but something unexpected doesn't happen in the presence of type errors.


i would certainly expect it to go through the charset in order instead of jumping back to a, but if you think of a string as a base 256 (or 26 in this case) number then it makes sense




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: