A friend said that these two are actually the difference between the wording, the following I would like to show you about the difference between global and $globals, the need for a friend can be specific reference.
According to the official explanation,
1. $GLOBALS [' var '] is the external global variable itself.
2.global $var is a reference or pointer to an external $var with the same name.
Let's look at the following example:
To illustrate:
| The code is as follows |
Copy Code |
$var 1 = 1; $var 2 = 2; function Test () { $GLOBALS [' var2 '] = & $GLOBALS [' var1 ']; } Test (); echo $var 2; ?> |
Normal print result is 1
| The code is as follows |
Copy Code |
$var 1 = 1; $var 2 = 2; function Test () { Global $var 1, $var 2; $var 2 = & $var 1; } Test (); echo $var 2; ?>
|
Unexpected print result is 2
Why would I print a result of 2? This is because $VAR1 's reference points to the $VAR2 's reference address. Cause the value of the substance has not changed
We all know that the variables generated by the functions in PHP are private variables of the function, then the variables generated by global are determined not to escape the rule, why do you say, look at the following code:
PHP code
| The code is as follows |
Copy Code |
function Test () { Global $a; unset ($a); } $a = 1; Test (); Print $a; ?> |
Performance results are:
1
Why does it output 1? Didn't you give $ A to unset? Unset failed? PHP bug?
is not, in fact, the unset function, is to the test of a $ unset lost, you can participate in the back of the function
Print $a;
That is, global has generated the alias variable "$a" of $ A outside the test function, in order to differ from the outside $ A
Then go back to Example 1 above, see Test_global in the Code "$var 2 =& $var 1;", above is a reference assignment operation, that is, $VAR2 will point to the physical memory address pointed to by var1
So we come to the conclusion that the difference between global and $globals[in a function is:
Global in the function produces a pointer to the function external variable alias variable, but not the real function external variable, one but changed the alias variable's point address, will produce some unexpected situation, for example 1.
$GLOBALS [] Confirm that the call is an external variable, and the function will always be consistent inside and outside!
You can control the following two sub-columns:
| The code is as follows |
Copy Code |
Global function MyFunction () { Global $bar; Unset ($bar); } $bar = "someting"; MyFunction (); Echo $bar; ?> Output: someting $global []: function foo () { unset ($GLOBALS [' Bar ']); } $bar = "something"; Foo (); Echo $bar; ?> Output: Empty |
PHP's global variables and C language are a little different, in C, the global variables in the function of the active, unless the local variable shrouded. This may cause some problems, and some people may inadvertently change a global variable. A global variable in PHP must be declared global when applied in a function.
Examples of applying global
| The code is as follows |
Copy Code |
$a = 1; $b = 2; function Sum () { Global $a, $b; $b = $a + $b; } Sum (); Echo $b; ?> |
The output of the above script will be "3". The global variable $a and $b are declared in the function, and all reference variables of any variable are pointed to the global variable.
Let's look at one more example.
| The code is as follows |
Copy Code |
$var 1 = 1; function Test () { unset ($GLOBALS [' var1 ']); } Test (); echo $var 1; ?> |
Because $var1 was removed, nothing was printed.
| The code is as follows |
Copy Code |
$var 1 = 1; function Test () { Global $var 1; Unset ($var 1); } Test (); echo $var 1; ?> |
Accidentally printed 1. To prove that the deletion is only an alias | reference, the value of itself has not been changed.
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.
http://www.bkjia.com/PHPjc/632142.html www.bkjia.com true http://www.bkjia.com/PHPjc/632142.html techarticle A friend said that these two are actually the difference between the wording, the following I would like to show you about the difference between global and $globals, the need for a friend can be specific reference. According to the official solution ...