Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Created March 31, 2013 18:08
Show Gist options
  • Select an option

  • Save philsturgeon/5281485 to your computer and use it in GitHub Desktop.

Select an option

Save philsturgeon/5281485 to your computer and use it in GitHub Desktop.
PHP, you crazy bitch.
php > $i = '';
php > var_dump(++$i);
string(1) "1"
php > var_dump(++$i);
int(2)
php > var_dump(++$i);
int(3)
@philsturgeon
Copy link
Copy Markdown
Author

@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:

php > $x = "a";
php > var_dump(++$x);
string(1) "b"

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.

@dragoonis
Copy link
Copy Markdown

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

@philsturgeon
Copy link
Copy Markdown
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