This article provides a detailed introduction to the global variables in PHP, and there are a few friends who need to know how to use the Globals function to refer to this article.
The scope of a variable is the context in which it is defined (that is, its effective scope). Most PHP variables have a single range. This separate scope span also contains the files introduced by include and require. For example:
The code is as follows |
Copy Code |
$a = 1; Include ' B.inc '; ?> |
Here the variable $a will take effect in the include file B.inc. However, in a user-defined function, a local function scope is introduced. Any variables that are used inside the function will be limited to the local function in the default context. For example:
The code is as follows |
Copy Code |
$a = 1; /* Global scope */ function Test () { echo $a; /* Reference to local scope variable */ } Test (); ?> |
This script does not have any output, because the Echo statement refers to a local version of the variable $a, and within that range, it is not assigned a value. You may notice that the global variables in PHP are a little different from the C language, and in C, global variables are automatically applied in functions unless overridden by local variables. This can cause some problems, and some people may accidentally change a global variable. In PHP, global variables must be declared as globals when used in functions.
Today, we encountered a problem where the PHP global variables did not work.
Let's start with a simple code:
The code is as follows |
Copy Code |
$a = 0; function Test () { $a = 1; } Test (); echo $a; ?> |
The output in the above code is 0, because the $ A variable in the function body test is defaulted to a local variable, and the scope of the $a is within test. Modify the code as follows
The code is as follows |
Copy Code |
$a = 0; function Test () { Global $a;//declare that the $ A variable used within the function body test is global $a = 1; } Test (); echo $a; ?>
|
Declare that the $ A variable used within the function body test is the global global variable, so that $ A has the overall effect, so the output is 1.
The above example is just the basic global variables knowledge, below we look at the complex point:
a.php file
The code is as follows |
Copy Code |
function Test_global () { Include ' b.php '; Test (); } $a = 0; Test_global (); echo $a; ?> b.php file function Test () { Global $a;//declare that the $ A variable used within the function body sum is global $a = 1; } ?> |
Why is the output 0?!!
In a user-defined function, a local function scope is introduced. Any variables that are used inside the function will be limited to the local function (including variables within the import file of include and require) by default!
Explanation: The internal test_global of the a.php file is a defined third-party function that imports $ A in the global global variable of $ A in the b.php file with include, so $ A is limited to the Test_global local function, so the b.php file is within the $ The scope of a is within the test_global, not the whole a.php ....
Solution:
1. Punch out the local function
The code is as follows |
Copy Code |
a.php file function Test_global () { Test (); } Include ' b.php '; To remove an include from a local Test_global function $a = 0; Test_global (); echo $a; ?> b.php file function Test () { Global $a; $a = 1; } ?> |
2. Excellent access to the viewer
The code is as follows |
Copy Code |
a.php file Include ' b.php '; $a = 0; Set_global ($a); echo $a; ?> b.php file Function Set_global (& $var) { $var = 1; } ?> |
http://www.bkjia.com/PHPjc/628915.html www.bkjia.com true http://www.bkjia.com/PHPjc/628915.html techarticle This article provides a detailed introduction to the global variables in PHP, and there are a few friends who need to know how to use the Globals function to refer to this article. The scope of the variable is the context background it defines ...