A global variable for PHP if the address of a local variable is referenced
$b = "DASDF";
Function abc () {
$a = "ABC";
Global $b;
$b = & $a;
echo $b. " inner; ";
}
ABC ();
echo $b. ";";
When I output B inside the function, it's ABC, but after we leave the function, output B, and revert to the initial value of DASDF, what's going on? Supposedly, the address of a local variable is quoted, and when the local variable is destroyed, the value of B should be empty.
------to solve the idea----------------------
In the manual:
http://www.php.net/manual/zh/language.references.whatdo.php#example-251
if a variable declared as global is assigned to a reference within a function, the reference is only visible inside the function. You can avoid this by using an array of $GLOBALS.
The reasons are as follows:
take global $var as a shorthand for $var =& $GLOBALS [' var ']; Thus assigning other references to $var only changes the reference to the local variable.
------to solve the idea----------------------
This post was last edited by xuzuning on 2012-08-28 09:21:48
$b = "DASDF";
Function abc () {
$a = "ABC";
Global $b; This is a reference to the $GLOBALS [' B ']
$b = & $a;
$GLOBALS [' bb '] = & $a;
echo $b. " inner; ";
}
ABC (); Abcinner;
echo $b. ";"; DASDF;
Echo $BB; Abc
------to solve the idea----------------------
Remember the two concepts of PHP, all variables are pointers, all scopes are different variables are different variables, you can