the output is 0 5,but i don’t know why,who can tell me the difference of global and $GLOBALS,and why the results of the example are 0 5,not 5 5,any tips would be appreciated!
$GLOBALS is is a variable that represents the global scope, so everything you change in there will be changed in the global scope.
Note that changing $GLOBALS is considered to be poor style, for the same reasons changing public variables of an object outside of that object is considered poor style.
(Some might say it’s considered harmful, I won’t go that far).
This does not hover create a pointer from the $var1 inside your function to the $var1 in your global scope, meaning that changing $var1 in your function does not change $var1 in the global scope.
Changing $var2 =& $var1 to $var2 = $var1 does indeed change the global scope.
Explanation is in the section “References with global and static variables” of the manual link I just posted.
I’m quite puzzled why it doesn’t work (seems to be a valid statement to me), but I can live with the fact that it doesn’t. Hardly use & anyway
$var2 is actually a local variable that references $GLOBALS[‘var2’]. You can change $GLOBALS[‘var2’] via the local $var2 in the function, but if you assign it another reference then it no longer points to $GLOBALS[‘var2’], hence you can no longer change $GLOBALS[‘var2’], rather you will change what it now points to.
function test_global() {
global $var1;
// this is what global does, perhaps more obvious?
$var2 =& $GLOBAL['var2'];
// local $var2 no longer points to $GLOBAL['var2']
$var2 =& $var1;
$var2 = 999; // $var1 outside the function is now set to 999
}
PHP has different variable scopes. A variable that is available in your “normal code”, i.e., not in a function, is said to be in the global scope.
A variable that is inside inside a function said to be inside the function scope.
These scopes are disjunct, meaning a variable that lives in the global scope can not be referred to from a function direcly, and vice versa.
If you want to use a variable from the global scope inside a function, you need to use the “globals” keyword.
When you use the “globals” keyword, don’t use $var1 =& $var2, but use $var1=$var2 (since $var1=&$var2 doesn’t work).
Think of it this way. There is the world, in this world there is your house.
People that are inside your house can not be in the rest of the world at the same time, and vice versa. However, from your house you can telephone someone that is currently not in your house.
That is what “globals” does, it “telephones” from a function (your house) to the global scope (the rest of the world).
Try and understand the above first, when you do, I’m sure the $GLOBALS will become clear to you
got it,and the example you made is much visualize.thank you very very much, but i still don’t understand why $var1=&$var2 doesn’t work and $var1 =& $GLOBAL[‘var2’] can,what the difference between the two,any tips would be appreciated,the more detail,the better.
This is not an easy thing to give a simple explanation for, if you’re looking for an explanation that doesn’t fail in certain situations. Even though most of the time, php blurs the distinction between a variable and a value, they are very distinct.
Read the manual carefully. http://php.net/manual/en/language.references.php
Test your understanding frequently as you read.
I did a quick google for php references and there’s a few that draw some pictures and stuff that might help you.
If you want extremely fine grained details, I think you’re best off learning some of the details of php’s engine. It might be too advanced for you to currently understand though.
I’m not a big fan of using this feature of php. It’s helpful to use sometimes, but even then alternative is usually very viable as well.