$GLOBALS

$GLOBALS引用全局作用域中可用的全部变量

说明

一个包含了全部变量的全局组合数组。变量的名字就是数组的键。

范例

Example #1 $GLOBALS 范例

<?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();
?>

以上例程的输出类似于:

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

注释

Note:

“Superglobal”也称为自动化的全局变量。这就表示其在脚本的所有作用域中都是可用的。不需要在函数或方法中用global $variable; 来访问它。

Note: 变量可用性

与所有其他超全局变量不同,$GLOBALS在PHP中总是可用的。

add a note

User Contributed Notes 6 notes

up
28
therandshow at gmail dot com
4 years ago
As of PHP 5.4 $GLOBALS is now initialized just-in-time. This means there now is an advantage to not use the $GLOBALS variable as you can avoid the overhead of initializing it. How much of an advantage that is I'm not sure, but I've never liked $GLOBALS much anyways.
up
12
mstraczkowski at gmail dot com
2 years ago
Watch out when you are trying to set $GLOBALS to the local variable.

Even without reference operator "&" your variable seems to be referenced to the $GLOBALS

You can test this behaviour using below code

<?php
/**
* Result:
* POST: B, Variable: C
* GLOBALS: C, Variable: C
*/

// Testing $_POST
$_POST['A'] = 'B';

$nonReferencedPostVar = $_POST;
$nonReferencedPostVar['A'] = 'C';

echo
'POST: '.$_POST['A'].', Variable: '.$nonReferencedPostVar['A']."\n\n";

// Testing Globals
$GLOBALS['A'] = 'B';

$nonReferencedGlobalsVar = $GLOBALS;
$nonReferencedGlobalsVar['A'] = 'C';

echo
'GLOBALS: '.$GLOBALS['A'].', Variable: '.$nonReferencedGlobalsVar['A']."\n\n";
up
0
stevenjeffries at gmail dot com
5 months ago
I ran into the case where I needed to know if my script was in the global scope or not.

You can use $GLOBALS to figure out which case that is:

<?php // file foo.php

$some_unique_prefix_foo = "ok";
if (isset(
$GLOBALS["some_unique_prefix_foo"])) {
    echo
"Foo is in global scope.\n";
} else {
    echo
"Foo is NOT in global scope.\n";
}
unset(
$some_unique_prefix_foo);

// Inside another file.
function test() {
    include
"foo.html";
}
test();

?>

Outputs:

Foo is in global scope.
Foo is NOT in global scope.
up
-22
bkilinc at deyta dot net
3 years ago
I prefer accessing globals through static function calls. Source code looks better; I use glb::get('myglobalvar') instead of $GLOBALS['myglobalvar']. This gives me full control over global access, which can be the source of problems in practice.

class glb
{
    static public function set($name, $value)
    {
        $GLOBALS[$name] = $value;
    }

    static public function get($name)
    {
        return $GLOBALS[$name];
    }

}

$myglobalvar = 'Hello, World !';

function myfunction()
{
    $val = glb::get('myglobalvar');
    echo "$val\n";
    glb::set('myglobalvar', 'hi, again :)');
    $val = glb::get('myglobalvar');
    echo "$val\n";
}

myfunction();
up
-40
ravenswd at yahoo dot com
7 years ago
Keep in mind that $GLOBALS is, itself, a global variable. So code like this won't work:

<?php
   
print '$GLOBALS = ' . var_export($GLOBALS, true) . "\n";
?>

This results in the error message: "Nesting level too deep - recursive dependency?"
up
-77
Gratcy
3 years ago
this is technique that i always did for configuration file..

<?php
$conf
['conf']['foo'] = 'this is foo';
$conf['conf']['bar'] = 'this is bar';

function
foobar() {
    global
$conf;
   
var_dump($conf);
}

foobar();

/*
result is..

array
  'conf' =>
    array
      'foo' => string 'this is foo' (length=11)
      'bar' => string 'this is bar' (length=11)

*/
?>