PHP5 + introduced the concept of $ GLOBALS delayed initialization. author: zhanhailiang date: Today, I want to use $ GLOBALS [_ SERVER] to replace $ _ SERVER to access related environment variables, the error "_ SERVERundefined" is always reported. Use Case 1: & lt ;? Php & nbsp; print_r ($ GLOBALS); PHP5 + introduces the concept of $ GLOBALS delay initialization
Author: zhanhailiang date:
To use $ GLOBALS ['_ server'] to replace $ _ SERVER to access related environment variables, the error "_ SERVER undefined" is always reported. Use cases:
Case 1:
At this time, the output does not contain _ SERVER information:
Array( [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ))
Case 2:
The output contains _ SERVER information:
Array( [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) [_SERVER] => Array ( ) )
Check the description of $ GLOBALS in the PHP manual and reference the comments of therandshow at gmail dot com:
Therandshow at gmail dot com
As of PHP5.4 $ GLOBALS is now initialized just-in-time. This means there now is an advantage to not use
The $ GLOBALS variable as you can avoid the overhead of initializing it. How much of an advantage that is
I'm not sure, but I 've ve never liked $ GLOBALS much anyways.
Trace the root data source and find the PHP5Changelog update log description:
Unordered List ItemImproved Zend Engine, performance tweaks and optimizations
Unordered List ItemChanged $ GLOBALS into a JIT autoglobal, so it's initialized only if used. (this may affect opcode caches !)
718 ; When enabled, the SERVER and ENV variables are created when they're first 719 ; used (Just In Time) instead of when the script starts. If these variables 720 ; are not used within a script, having this directive on will result in a 721 ; performance gain. The PHP directives register_globals, register_long_arrays, 722 ; and register_argc_argv must be disabled for this directive to have any affect. 723 ; http://php.net/auto-globals-jit 724 auto_globals_jit = On
Finally, we can understand that when auto_globals_jit = On is enabled in PHP5 +, the $ _ SERVER variable and the $ _ ENV variable will not be created when the script is started, instead, it is created when $ SERVER and $ ENV are used for the first time. Therefore, the above two cases may occur.
Note:
Conclusion:
Auto_globals_jit setting is also affecting $ _ REQUEST superglobal in 5.3 It is not explicitly stated in documentation.
When auto_globals_jit = On is enabled at least in version 5.3.13, $ _ REQUEST is created only when it is used for the first time.