Interesting Use of PHP Super Globals

Web-hosting business can now be started for just $19.95 with unlimited resources. Start your own.

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 [1]. 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 cause their initialization.

What does it mean? Like I did in this example, these super global arrays have to appear at least once in the whole 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.
  1. $server = '_SERVER';
  2. //Will result in a Notice: Undefined variable: _SERVER ...
  3. print_r( $$server );
PHP Code

While this piece of code will work fine:
  1. $server = '_SERVER';
  2. $_SERVER;           //This does nothing more than forcing PHP to initialize _SERVER array
  3. print_r( $$server );//It Works...
PHP Code

Even inaccessible
$_SERVER
will make this work, because compiler saw it at compile time and made it available.
  1. $server = '_SERVER';
  2. if( false ) {
  3.      $_SERVER;
  4. }
  5. print_r( $$server );//It Works...
PHP Code

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:
  1. $server = '_SERVER';
  2. print_r( $$server );//It Still Works...
  3. die;
  4. $_SERVER;
PHP Code

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.
  1. $_SERVER;$_ENV;$_REQUEST;
PHP Code



  1. High Performance Hosting is now $3.96 a month
  2. How to validate ASCII Text - PHP
  3. Implementing QuickSort in PHP
  4. register_globals is Back - PHP Implementation
  5. Remove undesired characters with trim_all() - PHP
  6. Number-to-Word Conversion with PHP


© 2012-2017 PageConfig.com - Scripts - Twitter