[nycphp-talk] more on &$var in function
Chris Snyder
csnyder at chxo.com
Fri Sep 5 15:06:52 EDT 2003
Michael Southwell wrote:
> aha, you mean like this?
>
> $var1="something";
> $var2="something else";
> function dosomething($var1){
> global $GLOBALS;
> etc.
>
No need to do that, the $GLOBALS array is always available in the local
scope.
Here's an example:
// global variables -- these aren't going to change for this request
$goatcolor= "red";
$goatname= "Quux";
$barn= new Barn;
$barn->color= "green";
function findGoat($goatname) {
// make the current $barn object available locally
global $barn;
if ( $barn->color== $GLOBALS['goatcolor'] ) {
print "$goatname is in this barn.";
$barn->empty= 0;
}
else {
print "$goatname is not in the $barn->color barn.";
$barn->empty= 1;
}
}
I could make a better example. :-)
By referencing $goatcolor as $GLOBALS['goatcolor'], I'm much less likely
to change it by accident. And possibly the script is more readable, at
least to me.
csnyder
More information about the talk
mailing list
Automatic Email Organization without missing anything!