note 106691 deleted from reserved.variables.globals by thiago
| From: | thiago@php.net | Date: | Tue, 29 Nov 2011 13:00:53 +0000 |
| Subject: | note 106691 deleted from reserved.variables.globals by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184763@lists.php.net to get a copy of this message | ||
Note Submitter: nathan dot bishop at parasane dot net
----
@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.