Magically Disappearing Global Variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Albright
Forum Newbie
Posts: 20
Joined: Sat Sep 13, 2003 8:03 pm
Contact:

Magically Disappearing Global Variables?

Post by Albright »

I was having trouble accessing a global in one of my functions. The global is an array named $config. If you will, please note the following chunk of code...

Code: Select all

<?php
function drawhead($pgtitle) {
    print_r ($GLOBALS);
    print_r ($config);
    print_r ($GLOBALS);
// etc...
?>
And its output...

Code: Select all

Array
(
    &#1111;HTTP_POST_VARS] =&gt; Array
        (
        )

    &#1111;_POST] =&gt; Array
        (
        )

    &#1111;HTTP_GET_VARS] =&gt; Array
        (
        )

    &#1111;_GET] =&gt; Array
 ...etc...
    &#1111;config] =&gt; Array
        (
            &#1111;id] =&gt; 1
            &#1111;temp] =&gt; default
            &#1111;datefmt] =&gt; D j M Y g:i A
            &#1111;sitetitle] =&gt; Phanatix-Powered Site
            &#1111;lang] =&gt; en-us
            &#1111;maxarts] =&gt; 0
            &#1111;catsep] =&gt; ,
            &#1111;cxn] =&gt; Resource id #4
            &#1111;ver] =&gt; 0.001
            &#1111;pre] =&gt; 
        )
...etc...
&lt;br /&gt;
&lt;b&gt;Notice&lt;/b&gt;:  Undefined variable:  config in &lt;b&gt;/Users/albright/Sites/phanatix/incl/dohead.php&lt;/b&gt; on line &lt;b&gt;32&lt;/b&gt;&lt;br /&gt;
Array
(
    &#1111;HTTP_POST_VARS] =&gt; Array
...etc...
    &#1111;config] =&gt; Array
        (
            &#1111;id] =&gt; 1
            &#1111;temp] =&gt; default
            &#1111;datefmt] =&gt; D j M Y g:i A
            &#1111;sitetitle] =&gt; Phanatix-Powered Site
            &#1111;lang] =&gt; en-us
            &#1111;maxarts] =&gt; 0
            &#1111;catsep] =&gt; ,
            &#1111;cxn] =&gt; Resource id #4
            &#1111;ver] =&gt; 0.001
            &#1111;pre] =&gt; 
        )
...So PHP can see my variable, but it's not letting me do anything with it? What's going on here, and how can I fix it?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

$GLOBALS is a "superglobal" ie it can be acessed anywhere from any scope.

$config is itself just an ordinary array in the global scope. It doesn't exist within the function scope unless you pass it in or access it directly from the superglobal $GLOBALS:

Code: Select all

<?php
print_r($GLOBALS['config']);
?>
Last edited by McGruff on Wed Aug 10, 2005 5:12 pm, edited 1 time in total.
User avatar
phplancer.com
Forum Newbie
Posts: 2
Joined: Sun Sep 28, 2003 9:01 am
Location: Preston, UK

Re: Magically Disappearing Global Variables?

Post by phplancer.com »

What about the register_globals?
"Perhaps the most controversial change in PHP is when the default value for the PHP directive register_globals went from ON to OFF in PHP 4.2.0."
http://php.planetmirror.com/manual/en/s ... lobals.php
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: Magically Disappearing Global Variables?

Post by McGruff »

phplancer.com wrote:What about the register_globals?
What did you want to ask about register globals?
User avatar
Albright
Forum Newbie
Posts: 20
Joined: Sat Sep 13, 2003 8:03 pm
Contact:

Post by Albright »

McGruff wrote:

Code: Select all

<?php
print_r($GLOBALS['config']);
?>
Hmm... This works, and $GLOBALS['config']['whatever'] lets me access the values in that array. Thank you.

Okay, so... global variables in PHP aren't really global, or... ? Why did the PHP team find the need to do globals different from every other programming lang in the universe? :P
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$GLOBALS are global, only the scope of the function is not ;-)
It's more like a namespace, the lookup does not bubble up from local to global scope.
Post Reply