Scope of global variables in PHP

Source: Internet
Author: User
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 a few tests, I suddenly remembered that it was not loading slowly.

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 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:

  1. $ My_var = 'Hello World ';
  2. Test_global ();
  3. Function test_global (){
  4. // Now in local scope
  5. // The $ my_var variable doesn't exist
  6. // Produces error: "Undefined variable: my_var"
  7. Echo $ my_var;
  8. // Now let's important the variable
  9. Global $ my_var;
  10. // Works:
  11. Echo $ my_var;
  12. }
  13. ?>

As you can see in the preceding example, the "global" keyword is used to import global variables. it seems that it works well and is very simple, so why do we need to worry about using the "global" keyword to define global data?

Let me explain the use of the global variable to the person at the entry. The global word in the term "global variable" already tells us that this variable can be used everywhere, first look at an instance:

  1. $ A = 1;
  2. $ B = 2;
  3. Function Sum ()
  4. {
  5. Global $ a, $ B; // declare it as a global variable
  6. $ B = $ a + $ B;
  7. }
  8. Sum ();
  9. Echo $ B;
  10. ?>

Result: 3

If no global variable is available, the values $ a and $ B cannot be obtained in the method. therefore, to use an external variable in the method, you must declare this variable as a global variable first, the code is as follows:

  1. $ W3sky = 1;
  2. $ W3sky2 = 2;
  3. Unction Sum ()
  4. {
  5. Global $ w3sky, $ w3sky2; $ w3sky2 = $ w3sky + $ w3sky2;
  6. } Sum ();
  7. Echo $ w3sky2;
  8. ?>

The output of the above script is "3". The global variables $ w3sky and $ w3sky2 are declared in the function. all referenced variables of any variable point to the global variable.

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.