Re: When is a global not a global?

From: Date: Wed, 12 Jul 2006 23:38:26 +0000
Subject: Re: When is a global not a global?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-239397@lists.php.net to get a copy of this message
On Wed, 2006-07-12 at 18:52, Nick Wilson wrote: > hi all, > > 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 above code is probably being included, and probably being included by a function and so $foo does not have global scope. To ensure global scope: <?php $GLOBALS['foo'] = 'bar'; function foobar() { global $foo; print( " ------ " . $foo ); exit; } foobar(); ?> Try that and let us know what happend. Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#239397) next »