Interesting Use of PHP Super Globals

When the php.ini directive auto_globals_jit is enabled, $_SERVER and $_ENV super global arrays are created at the time they are first used in the script, rather than being initialized at the start of script. The benefit of this directive is that these super global arrays are not created at all if not used within a script, resulting in a little performance gain.

$_REQUEST appears to be of the same category and is created just-in-time when the auto_globals_jit directive is turned On.

Something interesting written on php.net about these Just-In-Time variables is that usage of these variables is checked during the compile time so using them through variable variables or $GLOBALS will not initialize them.

What does it mean? Like I did in this example, these super global arrays have to appear at least once in the script file to enable their use through variable variables if the auto_globals_jit directive is ON. So, even if you merely put the names of these super global arrays at the end of the file, even after a die statement where they are never going to be used, or inside a definition of a function that is never going to be called, they are still created and can be used dynamically through variable variables.

$server = '_SERVER';
//Will result in a Notice: Undefined variable: _SERVER ...
print_r( $$server );

While this piece of code will work fine:

$server = '_SERVER';
$_SERVER;           //This does nothing more than forcing PHP to initialize _SERVER array
print_r( $$server );//It Works...

Even inaccessible $_SERVER will make this to work, because compiler saw it at compile time and made it available.

$server = '_SERVER';
if( false ) {
     $_SERVER;
}
print_r( $$server );//It Works...

Even if $_SERVER is used after a die statement where it is never reached, but visible to compiler at compile time, variable variables still work for it:

$server = '_SERVER';
print_r( $$server );//It Still Works...
die;
$_SERVER;

To dynamically access these super global arrays on a PHP installation where php.ini has auto_globals_jit enabled, you cannot disable the directive at run time and also you cannot access these arrays through $GLOBALS. Still, if you want to dynamically use super global arrays, you can look at the above suggested working examples or copy the below one line code anywhere in your main script file.

$_SERVER;$_ENV;$_REQUEST;