PHP: $GLOBALS example

This entry is part 13 of 54 in the series PHP Tutorial

$GLOBALS — References all variables available in global scope

It is an associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

function test() {
    $foo = "local variable";

    echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
    echo '$foo in current scope: ' . $foo . "\n";

$foo = "Example content";

The above example will output something similar to:

$foo in global scope: Example content
$foo in current scope: local variable


Series Navigation<< PHP: Superglobals

Leave a comment

Leave a Reply