note 106691 added to reserved.variables.globals
| From: | nathan dot bishop at parasane dot net | Date: | Tue, 29 Nov 2011 00:07:15 +0000 |
| Subject: | note 106691 added to reserved.variables.globals | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184746@lists.php.net to get a copy of this message | ||
@Anonymous
For one thing you can't use the 'global' keyword to define/manipulate what you wish
to include inside the function.
You would at most do;
global $foo;
Doing anything other you'll probably get an 'unexpected =' error.
The main difference between $GLOBALS superglobal variable and the global keyword is that, while you
can modify an external variable by doing say;
<?php
$foo = 'arr';
echo $foo . "\n";
function moo() {
$GLOBALS['foo'] = 'bar';
}
moo();
echo $foo . "\n";
?>
But not if you would do something such as;
<?php
$foo = 'arr';
echo $foo . "\n";
function moo() {
$foo = $GLOBALS['foo'];
$foo = 'bar';
echo $foo . "\n";
}
moo();
echo $foo . "\n";
?>
In this aspect, you would be creating $foo within the class, as a copy of the external $foo
variable, and modifying it only within the class.
However using the global keyword as such;
<?php
$foo = 'arr';
echo $foo . "\n";
function moo() {
global $foo;
$foo = 'bar';
}
moo();
echo $foo . "\n";
?>
You'll see that changes to $foo inside the function, will pass to the variable entirely.
The main difference is, the one in some opinions might be cleaner looking, as well as makes it
easier to modify variables in the global scope if you want to.
----
Server IP: 192.41.42.26
Probable Submitter: 96.247.146.250
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.globals.php
Edit -- https://master.php.net/note/edit/106691
Del: integrated -- https://master.php.net/note/delete/106691/integrated
Del: useless -- https://master.php.net/note/delete/106691/useless
Del: bad code -- https://master.php.net/note/delete/106691/bad+code
Del: spam -- https://master.php.net/note/delete/106691/spam
Del: non-english -- https://master.php.net/note/delete/106691/non-english
Del: in docs -- https://master.php.net/note/delete/106691/in+docs
Del: other reasons-- https://master.php.net/note/delete/106691
Reject -- https://master.php.net/note/reject/106691
Search -- https://master.php.net/manage/user-notes.php