Created
March 31, 2013 18:08
-
-
Save philsturgeon/5281485 to your computer and use it in GitHub Desktop.
PHP, you crazy bitch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| php > $i = ''; | |
| php > var_dump(++$i); | |
| string(1) "1" | |
| php > var_dump(++$i); | |
| int(2) | |
| php > var_dump(++$i); | |
| int(3) |
Author
Empty string casted to an integer is 0 dude. Lets move on :D
What is interesting is the "1" then int(2). I know why it does it though.
safe type juggling but no type casting
"" (using numeric operator) "0".
ok you really did mean to play with an existing numeric value, you're now and integer
"0" (using numeric operator) int(1).
Author
Yep, like I said I know what it's doing, but its fucking mental.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@AeroCross: That is perfectly expected and behaviour by design. Loose typing is brilliantly useful, and stops me having to write constant typecasting just because char[10] is not compatible with string, or some other C nonsense.
@SlKelevro: Damn right, strings with alphabetic characters will increase just like numeric characters:
Good feature, but not really whats happening here.
The original just makes me laugh, it's clearly thinking:
1.) "" is... well its not a character so it must be a number.
2.) Let's just pretend the value 0 (and its a string, so keep it as as string) and increment it to "1"
3.) "1" is numeric! Awesome I know how to do numeric increments.
4.) int(2)
It feels to me like step 2 is wrong, and everything else is great. If step 2 produced int(1) then it would make some sort of sense.