Re: When is a global not a global?

From: Date: Thu, 13 Jul 2006 18:49:41 +0000
Subject: Re: When is a global not a global?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-239430@lists.php.net to get a copy of this message
On Wed, July 12, 2006 5:52 pm, Nick Wilson wrote: > After upgrading a CMS, im having a problem with global variables not > showing up anymore -- configs and things could have changed, but > search > as i have, i cannot find anything to help me work out what the problem > is. > > This should work of course: > > $foo = 'bar'; > > function foobar() { > global $foo; > print(" ------ " . $foo); > exit; > } > > foobar(); > > It prints *nothing*. Does anyone have an idea as to what might stop > this > from functioning as expected? The only time I have seen this is when what REALLY happens is like this: <?php //The CMS code has something like: function cms_include_user_code ($file){ include $file; } ?> <?php //some of your code, pulled in through $file above: $foo = 'bar'; ?> <?php //More of your code, the $file above, has this: function foobar() global $foo; echo "in foobar, foo is $foo<br />\n"; } ?> So, if you work through it, you'll see that your $foo = 'bar'; happens *INSIDE* the cms_include_user_code function. So it's not that global isn't working. It's that you need: <?php global $foo; $foo = 'bar'; ?> in that first file, so that your $foo buried inside the CMS function is global, instead of local to their cms_include_user_code function. -- Like Music? http://l-i-e.com/artists.htm

« previous php.general (#239430) next »