$GLOBALS

(PHP 4, PHP 5, PHP 7)
References all variables available in global scope

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.

Notes:

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

Variable availability

Unlike all of the other superglobals, $GLOBALS has essentially always been available in PHP.

Examples:
$GLOBALS example
<?php
function test() {
    $foo = "local variable";

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

$foo = "Example content";
test();
?>

The above example will output something similar to:

$foo in global scope: Example content
$foo in current scope: local variable
doc_php
2016-02-24 15:53:11
Comments
Leave a Comment

Please login to continue.