Hudzilla.org - the homepage of Paul Hudson
Contents > Functions Wish List | Report Bug | About Me ]

4.17     Overriding scope with the GLOBALS array

This is NOT the latest copy of this book; click here for the latest version.

At some point in your PHP programming career you will want to read a global variable inside a function - I can pretty much guarantee that, because it is a very popular thing to do. Luckily, it is made easy for you by PHP through the $GLOBALS superglobal array, which allows you to access global variables even from within functions. When it comes to the $GLOBALS array it is quite simple: all variables declared in the global scope are in the $GLOBALS array, which you can access anywhere in the script.

To demonstrate this in action, consider the following script:

<?php
    
function foo() {
        
$GLOBALS['bar'] = "wombat";
    }

    
$bar = "baz";
    
foo();
    print
$bar;
?>

What do you think that will output this time? If you guessed "wombat", you would be correct - the foo() function literally alters a variable outside of its scope, so that even after it returns control back to the main script, its effect is still felt. You can of course read variables in the same way, like this:

$localbar = $GLOBALS['bar'];

However, that is quite hard on the eyes. PHP allows you to use a special keyword, GLOBAL, to allow a variable to be accessed locally. For example:

function myfunc() {
    GLOBAL
$foo, $bar, $baz;
    ++
$baz;
}

That would allow a function to read the global variables $foo, $bar, and $baz. The ++$baz line will increment $baz by 1, and this will be reflected in the global scope also.





<< 4.16 Variable scope in functions   4.18 Recursive functions >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
DoctoR JackaL - 13 Oct 2008

Also want to note that when defining a variable using GLOBAL , the varaible names are written infront of GLOBAL , while the values on a new line for each varaible , example :

<?php
GLOBAL $A, $B, $C;
$A = "This is variable A";
$B = "This is variable B";
$C = "This is variable C";
?>

DoctoR JackaL - 13 Oct 2008

I'm using PHP 5.05 and found many thingZ not said here .

1) using $GLOBALS inside or outside a function will make the variable a GLOBAL variable and can only be used outside the function in the script itself .

2) using GLOBAL inside a function makes the variable use-able inside and outside functions.

3) using GLOBAL outside functions makes the variable GLOBAL and can only be used outside the functions.

The important note is number 2 , try it urself and c , this iz what i always get

chima/chytons@yahoo.com - 13 Oct 2008

Note that when using the GLOBAL keyword to access a global variable from within a function, it's an error to simutaneously declare the variable GLOBAL and assign it a value as in:

function myFunc() {
GLOBAL $foo = somevalue;
}

The declaration and assigment should be done separately as in:

function myFunc() {
GLOBAL $foo;
$foo = somevalue;
}

fredrik at demomusic dot nu - 13 Oct 2008

Just a reminder that passing by reference and/or using the $GLOBALS array gives you more control of the scope as opposed to using the GLOBAL-command.

Also, a reminder of that constants are global in nature is approriate.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow