Scope of global variables in PHP _ PHP Tutorial

Source: Internet
Author: User
The scope of global variables in PHP. I recently developed a Discuz! Plug-in. today I encountered a problem. when I call the variable data of the plug-in a function in the background, a null value is returned. after several tests, I suddenly remembered that I was not developing a Discuz recently! Plug-in. today, I encountered a problem. when I call the variable data of the plug-in a function in the background, a null value is returned. after several tests, I suddenly remembered that it was caused by not loading the cache, therefore, the loadcache (\ 'Plug-In \ ') is added to load the plug-in cache. But the problem persists.

To analyze the problem, you need to call the data of A function (currently called function A) and have executed global $ _ G to call global variables, the $ _ G output through print_r also confirms that $ _ G contains data and the plug-in cache is missing. Function A also needs to be called in front of the platform, because the front-end has A plug-in. the php Shell has loaded the plug-in cache and does not need to execute loadcache ('plugin'). Therefore, executing the loadcache ('plugin') operation in the function affects the execution efficiency.

Analyze the problem again. the code that calls function A in the background is in another function (called function B for the time being). suddenly, I wonder if I want to execute global $ _ G in this function; so that the variable content loaded in the background is passed to function A by function B?

With a try, I added global $ _ G in function B. The result is successful!

This leads to the scope of the global scope. In this debugging, function A is called by function B, so the loadcache ('plugin') called in the background is only valid in the background, because global $ _ G is not used in function B, the latest $ _ G is not obtained. The file reference (require) of function A is written in function B. function A is A subset of function B. The global variable that runs loadcache in the background code is invalid for function, the $ _ G value obtained by global in function A bypasses the loadcache I wrote. Therefore, you need to run global in function B to obtain the latest $ _ G value to take effect in function.

The following is an example of using the "global" keyword:

The code is as follows:
$ My_var = 'Hello World ';
Test_global ();
Function test_global (){
// Now in local scope
// The $ my_var variable doesn't exist
// Produces error: "Undefined variable: my_var"
Echo $ my_var;
// Now let's important the variable
Global $ my_var;
// Works:
Echo $ my_var;
}
?>

As you can see in the preceding example, the "global" keyword is used to import global variables. It looks like it works

It is very easy, so why do we need to worry about using the "global" keyword to define global data?

Let me explain the use of global as the global variable to the person at the entry.

This variable can be used everywhere. let's look at an instance first:

The code is as follows:

$ A = 1;
$ B = 2;

Function Sum ()
{
Global $ a, $ B; // declare it as a global variable

$ B = $ a + $ B;
}

Sum ();
Echo $ B;
?>

Result: 3

If no global variable is available, the values of $ a and $ B cannot be obtained in the method. Therefore, if you want to use an external variable in the method

You need to declare this variable as a global variable so that it can be used.

The code is as follows:

$ W3sky = 1;
$ W3sky2 = 2;

Function Sum ()
{
Global $ w3sky, $ w3sky2; $ w3sky2 = $ w3sky + $ w3sky2;
} Sum ();
Echo $ w3sky2;
?>

The output of the above script is "3 ". Specify the global variables $ w3sky and $ w3sky2 in the function.

All referenced variables point to global variables.

Success! Plug-in. today, I encountered a problem. when I call the variable data of the plug-in a function in the background, a null value is returned. after a few tests, I suddenly remembered that it should not be...

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.