Recently in the development of a discuz! plug-in, I encountered a problem today, I in a function in the background to call the plug-in variable data return null value, test a few times after the unexpected thought should be not loaded cache caused, and joined the Loadcache (\ ' plugin\ ') loaded plug-in cache. But the problem remains.
To analyze the problem, the call to the data is a function (temporarily called function a), the global $_g has been executed, and the Print_r output $_g also confirms that there is data in the $_g, and the missing plugin cache. The function A is also called in the foreground, because the foreground covers the plugin.php shell, the plugin cache is loaded, the Loadcache (' plugin ') is not required, so performing the Loadcache (' plugin ') operation in the function has an effect on the execution efficiency.
To parse the problem again, the code behind the call to function A is in another function (called function B, for the time being), and suddenly wonder if the global $_g will be executed once in this function, so that the contents of the variables loaded in the background are passed by Function B to function a?
With a try attitude, I added the global $_g in function B, and the results were successful!
This gives an issue of the scope of the global scope. In my debugging, function A is called by Function B, so Loadcache (' plugin ') is called in the background, the new $_g generated is only valid in the background part, and in function B, because the global $_g is not used, the latest $_g is not obtained. And the file reference of function A (require) I wrote in function B, function A is a subset of function B, in the background code, the global variable that ran the Loadcache is not valid for function A, and the $_g value obtained by Global in function a bypassed the Loadcache I wrote. Therefore, it is necessary to run global in function B once to obtain the latest $_g value in order to take effect in function A.
Here's a demo example that uses the "global" keyword:
| The code is as follows |
Copy Code |
$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 example above, the "global" keyword is used to import global variables. It looks like it works
Well done, and very simple, so why do we have to worry about using the "global" keyword to define global data?
Let me explain to the people who are in the world the use of global variables, global variables, global two in this noun
The word has told us that this variable can be used in all places, first look at an example:
| The code is as follows |
Copy Code |
$a = 1; $b = 2; function Sum () { Global $a, $b; Declare it as a global variable inside $b = $a + $b; } Sum (); Echo $b; ?> |
Results: 3
If there is no global variable within the method, global cannot get a $ A, $b value, so in the method you want to use the outside variable
You need to declare this variable as a global variable, so you can use it, which is handy.
| The code is as follows |
Copy Code |
$w 3sky = 1; $w 3sky2 = 2; function Sum () { Global $w 3sky, $w 3sky2; $w 3sky2 = $w 3sky + $w 3sky2; }sum (); echo $w 3sky2; ?> |
The output of the above script will be "3". The global variables are declared in the function $w 3sky and $w 3sky2, all of any variables
Reference variables will point to global variables
http://www.bkjia.com/PHPjc/631549.html www.bkjia.com true http://www.bkjia.com/PHPjc/631549.html techarticle recently in the development of a discuz! plug-in, today encountered a problem, I in a function in the background to call the plug-in variable data return null value, test a few times after the failure of a sudden thought should be no ...