In fact all variables represent pointers that hold address of memory area with data that was assigned to this variable. When you assign some variable value by reference you in fact write address of source variable to recepient variable. Same happens when you declare some variable as global in function, it receives same address as global variable outside of function. If you consider forementioned explanation it's obvious that mixing usage of same variable declared with keyword global and via superglobal array at the same time is very bad idea. In some cases they can point to different memory areas, giving you headache. Consider code below:
error_reporting(E_ALL);$GLOB=0;
functiontest_references() {
global$GLOB;// get reference to global variable using keyword global, at this point local variable $GLOB points to same address as global variable $GLOB$test=1;// declare some local var$GLOBALS['GLOB'] = &$test;// make global variable reference to this local variable using superglobal array, at this point global variable $GLOB points to new memory address, same as local variable $test$GLOB=2;// set new value to global variable via earlier set local representation, write to old addressecho"Value of global variable (via local representation set by keyword global):$GLOB
";// check global variable via local representation => 2 (OK, got value that was just written to it, cause old address was used to get value)echo"Value of global variable (via superglobal array GLOBALS):$GLOBALS[GLOB]
";// check global variable using superglobal array => 1 (got value of local variable $test, new address was used)echo"Value ol local variable \$test:$test
";// check local variable that was linked with global using superglobal array => 1 (its value was not affected)global$GLOB;// update reference to global variable using keyword global, at this point we update address that held in local variable $GLOB and it gets same address as local variable $testecho"Value of global variable (via updated local representation set by keyword global):$GLOB
";// check global variable via local representation => 1 (also value of local variable $test, new address was used)}test_references();
echo"Value of global variable outside of function:$GLOB
";// check global variable outside function => 1 (equal to value of local variable $test from function, global variable also points to new address)?>