Phpglobal global variables-php Tutorial

Source: Internet
Author: User
Phpglobal global variables make it easy to hide php global variables.

For example, if a simple php configuration file is as follows:

$name="Yi_Zhi_Yu";

When we want to use this $ name, we all know to use include. That's right. But what if I want to use this $ name as a global variable to facilitate unified modification?

It is also easy:

include("./config.inc");global $name;echo $name; //Yi_Zhi_Yu

So the question is, if I want to use this global variable in a function, maybe you will write it like this.

Function test () {include ("./config. inc"); // The loaded global $ name; var_dump ($ name);} test () is incorrect ();

Sorry, you don't get the variable worth it, and the only thing you get is null.

This is a pitfall I encountered in reality, that is, the initialization of the global variables loaded by the file can only be in the outermost layer of the entire script, the outermost layer is that it cannot be contained in any function body or class.

Include (". /config. inc "); // The outermost layer initializes the global variable function test () {global $ name; // defines the global attribute echo $ name when using it; // Yi_Zhi_Yu} test ();

Another problem is that a variable with global attributes defined in test (assuming $ age = 25), if the value is modified outside ($ age = 27 ), then this variable ($ age) is called in another function. which value should this $ age be at this time? the code is as follows: it is the value at initialization (25 ), or the modified value (27) outside.

Function test () {global $ age; $ age = 25;} function test2 () {global $ age; var_dump ($ age); // What should this value be ?} Test (); var_dump ($ age); $ age = 27; test2 ();

Yes, this value should be 27. this is a global variable, but this definition and initialization are completed in a function (however, this method is not recommended, it is generally initialized in the configuration file I mentioned above. it is used only to define its global attributes, but does not modify its values in the code. it is only obtained as a configuration item ).

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.