Foreach All the Super Globals

This snippet is pretty great. It serves absolutely no purpose whatsoever and it simply silly.

foreach ($_GET as $key => $val) $$key=$val;
foreach ($_POST as $key => $val) $$key=$val;
foreach ($_SERVER as $key => $val) $$key=$val;
foreach ($_FILES as $key => $val) $$key=$val;
foreach ($_SESSION as $key => $val) $$key=$val;
foreach ($HTTP_SESSION_VARS as $key => $val) $$key=$val;

The main thing that is silly about this is that each of these super globals is already setup in key, value pairs (which is what this foreach is doing). Second, this snippet is running a foreach through each of the super globals, but it isn’t storing the data in a new array, it’s simply creating a new variable each time, meaning only the very last item in each super global will be available after the foreach.

Submitted by Sean Barton.


7 thoughts on “Foreach All the Super Globals

  1. Isn’t it using a variable variable? When the loop is run over `$_GET[‘some_key’]` (where ‘some value’ is stored in that key), it will produce `$some_key` with ‘some value’ stored inside it.

    So basically a brute force `extract()` on all of the super globals’ contents. Still not useful at all … then again, neither is `extract()`.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s