Weird $GLOBALS behavior

This question is purely theoretical, because I don’t actually need this code (and wouldn’t even want to anyway because it’s pretty ugly).
Anyway, I was just toying around a bit this afternoon (basically to see if ++$i++ would work, which it doesn’t :(), and after a suggestion of my colleague ended up with

$i = 0;
var_dump($GLOBALS); // shows there is a key '1' with value 1
var_dump($GLOBALS['1']); // null
var_dump($GLOBALS[1]); // null
var_dump(${1}); // int (1)

now, ++$i is 1, so I’m setting the variable $1 which, of course, is illegal because a variable can’t start with a number, but this works (i.e. no warnings, errors, etc).
When you then var_dump($GLOBALS), you will see that in that array there is a key 1 with a value 1. However, when you try and var_dump this (either using string or int for the key), PHP claims it doesn’t exist (null).
${1} on the other hand, does.

Does anyone know why $GLOBALS[‘1’] doesn’t work? I really think it should! :slight_smile:

PS. This also doesn’t work if you start with ${'1'} = 1 instead of ${++$i}++;, but that doesn’t look as fancy :cool:

Out of curiousity, if you var_dump(array_keys($GLOBALS)), does it give 1 as an int or a string?

as a string.

Well it is undefined behavior so…

Might it have something to do with $GLOBALS being internally recursive? It’s a weird variable to be sure with some special implementation behind it in the engine.