Page 1 of 1

Magically Disappearing Global Variables?

Posted: Sun Sep 28, 2003 3:42 am
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?

Posted: Sun Sep 28, 2003 4:56 am
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']);
?>

Re: Magically Disappearing Global Variables?

Posted: Sun Sep 28, 2003 9:01 am
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

Re: Magically Disappearing Global Variables?

Posted: Sun Sep 28, 2003 3:11 pm
by McGruff
phplancer.com wrote:What about the register_globals?
What did you want to ask about register globals?

Posted: Mon Sep 29, 2003 1:02 am
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

Posted: Mon Sep 29, 2003 1:09 am
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.