$_SESSION doesn't allow numeric keys in PHP < 5.4

I answered this question on stackoverflow the other day and it led me to question why $_SESSION[1] = 'Something'; doesn't work in PHP < 5.4 (5.3, 5.2, etc...). Give it a try, it should give you a notice along the lines of

Notice: Unknown: Skipping numeric key 1 in Unknown on line 0

I found this bug report when I was researching which says that the reason it throws the notice is because "false/true/null/etc are not valid key names". To me that isn't a great answer though because normal arrays can have numeric keys. Luckily Wiseguy replied to my answer and pointed out that the PHP docs used to say

The keys in the $_SESSION associative array are subject to the same limitations as regular variable names in PHP, i.e. they cannot start with a number and must start with a letter or underscore. For more details see the section on variables in this manual. I guess this was a side effect of register_globals. Curiously, the docs no longer state this. Perhaps this limitation was removed when the register_globals option was removed in PHP 5.4.0

I wasn't sure how register_globals fit into this though. I did some digging and was close to posting on stackoverflow to get some feedback about it! So I started working from a different angle, forget about it being a $_SERVER specific issue, maybe that didn't have anything to do with it. That landed me on the super globals page.

Super Globals

Not to regurgitate info, but super globals are global variables in scope in any part of a script without the need to prepend them with a 'global' keyword. With register_globals on PHP would create variables for all of the child items in the super global. For instance:

$_SERVER['Levi'] = 'Levi Jackson';

This would create a variable $Levi that has the value of 'Levi Jackson'. So if you had set a key of $_SERVER[1] = 'Levi Jackson' it would try and create a variable $1 set to 'Levi Jackson'. Which if you are familiar with variable basics is a big "no no".

PHP 5.4

Not that PHP 5.4 removed support for register_globals you can now use numeric keys with your super globals like $_SESSION. Enjoy.

If you have any feedback for me, I'd love to hear it - corrections, alternative paths, you name it! Send me an email levi@levijackson.xyz