note 118666 added to language.variables.scope
| From: | gried at NOSPAM dot nsys dot by | Date: | Sat, 16 Jan 2016 21:12:52 +0000 |
| Subject: | note 118666 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204698@lists.php.net to get a copy of this message | ||
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:
<?php
error_reporting(E_ALL);
$GLOB = 0;
function test_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 address
echo "Value of global variable (via local representation set by keyword global): $GLOB
<hr>";
// 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]
<hr>";
// check global variable using superglobal array => 1 (got value of local variable $test, new
address was used)
echo "Value ol local variable \$test: $test <hr>";
// 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 $test
echo "Value of global variable (via updated local representation set by keyword global):
$GLOB <hr>";
// 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 <hr>";
// check global variable outside function => 1 (equal to value of local variable $test from
function, global variable also points to new address)
?>
----
Server IP: 72.52.91.14
Probable Submitter: 93.85.50.104
----
Manual Page -- http://php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/118666
Del: integrated -- https://master.php.net/note/delete/118666/integrated
Del: useless -- https://master.php.net/note/delete/118666/useless
Del: bad code -- https://master.php.net/note/delete/118666/bad+code
Del: spam -- https://master.php.net/note/delete/118666/spam
Del: non-english -- https://master.php.net/note/delete/118666/non-english
Del: in docs -- https://master.php.net/note/delete/118666/in+docs
Del: other reasons-- https://master.php.net/note/delete/118666
Reject -- https://master.php.net/note/reject/118666
Search -- https://master.php.net/manage/user-notes.php