PHP5 + introduces the concept of $ GLOBALS delayed initialization _ PHP Tutorial

Source: Internet
Author: User
PHP5 + introduces the $ GLOBALS delay initialization concept. To use $ GLOBALS [_ SERVER] instead of $ _ SERVER to access related environment variables, the _ SERVERundefined error is always reported. Use Case 1 :? Phpprint_r ($ GLOBALS) today I want to use $ GLOBALS ['_ server'] to replace $ _ SERVER to access related environment variables. the error "_ SERVER undefined" is always reported. Use cases:

Case 1:

Print_r ($ GLOBALS );

At this time, the output does not contain _ SERVER information:

Array

(

[GLOBALS] => Array

* RECURSION *

[_ POST] => Array

(

)

[_ GET] => Array

(

)

[_ COOKIE] => Array

(

)

[_ FILES] => Array

(

)

)

Case 2:

Print_r ($ GLOBALS );

Print_r ($ _ SERVER );

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 PHP 5.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 !) Www.2cto.com

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

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.

Undefined error. Use Case 1 :? Php print_r ($ GLOBALS )...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.