note 119366 added to language.variables.scope
| From: | Ganlv | Date: | Sat, 21 May 2016 16:50:18 +0000 |
| Subject: | note 119366 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205532@lists.php.net to get a copy of this message | ||
<?php
$var = 1;
function foo() {
$var = &$GLOBALS['var'];
var_dump($var);
}
function bar() {
global $var; // they are the same.
var_dump($var);
}
foo();
bar();
var_dump($var);
?>
In a function, 'global $var;' is to declare a local variant, and the local $var has the
same reference to the global $var.
<?php
$var = 1;
function foo() {
global $var;
unset($var); // unset local $a, the global $a is still there.
var_dump($var); // Undefined variable: var
var_dump($GLOBALS['var']); // this is ok.
}
foo();
var_dump($var); // this is ok.
?>
<?php
$var = 1;
function bar() {
global $var;
unset($GLOBALS['var']); // unset global $a, the local $a is still here.
var_dump($var); // this is ok.
var_dump($GLOBALS['var']); // Undefined index: var
}
foo();
var_dump($var); // Undefined variable: var
?>
'unset($var);' is like 'var = NULL;'(var is a pointer) in the C language,
instead of 'free(var);'
----
Server IP: 72.52.91.14
Probable Submitter: 115.154.53.67
----
Manual Page -- http://php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/119366
Del: integrated -- https://master.php.net/note/delete/119366/integrated
Del: useless -- https://master.php.net/note/delete/119366/useless
Del: bad code -- https://master.php.net/note/delete/119366/bad+code
Del: spam -- https://master.php.net/note/delete/119366/spam
Del: non-english -- https://master.php.net/note/delete/119366/non-english
Del: in docs -- https://master.php.net/note/delete/119366/in+docs
Del: other reasons-- https://master.php.net/note/delete/119366
Reject -- https://master.php.net/note/reject/119366
Search -- https://master.php.net/manage/user-notes.php