The role of 1:global is to define global variables, but this global variable is not applied to the entire site, but is applied to the current page, including all files of include or require.
<?php $a = 123; function aa () {Global $a;//If you do not define a $ A as a Global variable, the body of functions cannot access a $ A outside of the function body, but you can define an identical name $ A, at which point the variable is a local variable, equivalent to the local variable of the C language. Can only be used inside the function body. echo $a; } AA ();?>
Summary: Global variables defined in the function body can be used outside the body, and global variables defined outside the function body cannot be used within the function body.
$global $a; $a = 123; function f () {echo $a;//Error,}//See the following example, function f () {global $a; $a =123;} f (); echo $a; Correct, you can use
2:global Problem Analysis:
Question: I defined some variables ($a) in the config.inc.php, and in other files the function external include ("config.inc.php"), the function internal need to use these variable $ A, if there is no declaration, echo $ A is not printing anything. So declaring the global $a, but there are a lot of functions and a lot of variables, you can't always repeat this statement? If there is any good solution, please advise.
Answer1: Define constants in config.inc.php first: define (constant name, constant value)
Require ' config.inc.php ' in other places that need to be used,
You can then use this constant directly in this file.
Answer2: I also have a way of defining arrays, such as $x[a], $x, so just declare the global $x one.
Answer3: I tried this method of yours, No.
Answer4: Change your php.ini file.
3. Some examples of global and $globals arrays
Example: Using Global
<?php $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 $w 3sky and $w 3sky2 are declared in the function, and all reference variables of any variable are pointed to the global variable. PHP has no restrictions on the maximum number of global variables a function can declare.
The second way to access variables globally is to customize $GLOBALS arrays with special PHP. The previous example can be written as:
Example using $GLOBALS instead of global
<? PHP $w 3sky = 1; $w 3sky2 = 2;function Sum () {$GLOBALS [' W3sky '] = $GLOBALS [' W3sky '] + $GLOBALS [' W3sky2 '];} Sum (); echo $w 3sky2;?>
In the $GLOBALS array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the contents of the variable. $GLOBALS exists globally because $GLOBALS is a hyper-global variable. The following example shows the usefulness of a hyper-global variable:
Examples of super global variables and scopes are shown
<? PHP function Test_global () {///Most of the predefined variables are not "super", they need to use the ' global ' keyword to make them valid in the local area of the function. Global $HTTP _post_vars;echo $HTTP _post_vars[' name '];//superglobals are valid in any scope and do not require ' global ' claims. Superglobals was introduced in PHP 4.1.0. echo $_post[' name ']; }?>
Global means that in a file you declare as global $db then you can refer to this $db under the statement.
4. The original thought that the global and $globals in addition to the wording not the same, others are the same, but in the actual application found that the difference between 2 is very big!
Let's look at the following example:
<?php//Example 1 function Test_global () {global $var 1, $var 2; $var 2 =& $var 1;} function Test_globals () {$GLOBALS [' VA R3 '] =& $GLOBALS [' var1 ']; } $var 1 = 5; $var 2 = $var 3 = 0; Test_global (); Print $var 2. " \ n "; Test_globals (); Print $var 3. " \ n ";?>
The result of the execution is:
0 5
How could this be? Shouldn't it be 2 5? How can there be 1 0 and a 5?
Well, we keep the above questions in-depth analysis of $globals and global principles!
We all know that variables are actually "codes" in the corresponding physical code, assuming that the memory allocated by the 3 variables we declared above is represented as:
Refer to the PHP manual for $globals Explanation:
Global variable: $GLOBALS
Note: $GLOBALS are available in PHP 3.0.0 and later versions.
An array that consists of all defined global variables. The variable name is the index of the array.
This is a "superglobal", or can be described as an automatic global variable.
In other words, $var1 and $globals[' var1 ' in the above code refer to the same variable, not 2 different variables!
What does global do now?
We all know that the variables generated by the functions in PHP are private variables of the function, then the variables generated by the Global keyword will certainly not escape the rule, why do you say, look at the following code:
<?php//Example 2 function test () {global $a; unset ($a);} $a = 1; Test (); Print $a;?>
The
Execution result is: 1
Why does it output 1? Didn't you give $ A to unset? Unset failed? PHP bug?
is not, in fact, unset work, is the test function of $ A to unset off, you can add
Print $a after the function;
Copy the code to test! That is, global generates the alias variable "$a" of $ A outside the test function, and test_globals after it has been executed, look at the variable.
At this point you can understand why example 1 after execution, $var 2 is 0, and $VAR3 is 5!
So we come to the conclusion that the difference between global and $globals[in a function is that
Global generates an alias variable that points to the external variable of the function in the function, not the actual external variable of the function, but changes the pointing address of the alias variable. There will be some unexpected situations (why is the print result 2?). This is because $VAR1 's reference points to the $VAR2 's reference address. Causes the value of the substance to not change. At this point the pointer to $var1 points to the $var2 pointer, but the pointer changes, but in essence there is no change in the value of $VAR2, so the value of $VAR2 will remain unchanged), such as Example 1.
$GLOBALS [] The actual call is an external variable, and inside and outside the function will always be consistent!