CakeFest 2024: The Official CakePHP Conference

$GLOBALS

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

说明

关联数组 array,包含当前脚本内定义成全局范围的所有变量的引用。 数组的键就是变量的名字。

示例

示例 #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

警告

从 PHP 8.1.0 起,不再支持对整个 $GLOBALS 数组的写访问:

示例 #2 写入整个 $GLOBALS 将会导致错误。

<?php
// 生成编译时错误:
$GLOBALS = [];
$GLOBALS += [];
$GLOBALS =& $x;
$x =& $GLOBALS;
unset(
$GLOBALS);
array_pop($GLOBALS);
// ...以及对 $GLOBALS 的任何其他写入/读写操作
?>

注释

注意:

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

注意: 变量可用性

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

注意:

从 PHP 8.1.0 起, $GLOBALS 现在是全局符号表的只读副本。 也就是说,全局变量不能通过副本进行修改。 在之前的版本中,$GLOBALS 数组和 PHP 数组通常传值的行为不一样,全局变量可通过副本修改。

<?php
// PHP 8.1.0 之前
$a = 1;
$globals = $GLOBALS; // 表面意义的按值复制
$globals['a'] = 2;
var_dump($a); // int(2)

// 从 PHP 8.1.0 起
// 这不再修改 $a。先前的行为违反了按值语义。
$globals = $GLOBALS;
$globals['a'] = 1;

// 要恢复以前的行为,请迭代其副本并将每个属性分配回 $GLOBALS。
foreach ($globals as $key => $value) {
$GLOBALS[$key] = $value;
}
?>

add a note

User Contributed Notes 4 notes

up
20
inafeeur at gmail dot com
1 year ago
We can be more clear with the extension of the given example.

Before PHP Version 8.1

<?php
$a
= 1;
$globals = $GLOBALS;
$globals['a'] = 2;
echo
$a; // 2
echo $globals['a']; // 2
echo $GLOBALS['a']; // 2
?>

After PHP Version 8.1

<?php
$a
= 1;
$globals = $GLOBALS;
$globals['a'] = 2;
echo
$a; // 1
echo $globals['a']; // 2
echo $GLOBALS['a']; // 1
?>
up
17
therandshow at gmail dot com
12 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
16
mstraczkowski at gmail dot com
10 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
13
vittorio.zamparella at famous googlemail
7 years ago
I finally found information about superglobals not being found in $GLOBALS:

https://bugs.php.net/bug.php?id=65223&edit=2
-------------------------------------
[2013-07-09 12:00 UTC] johannes @php.net
[...]super-globals (aka. auto globals) are not added to symbol tables by default for performance reasons unless the parser sees need. i.e.

<?php
$_SERVER
;
print_r($GLOBALS);
?>

will list it. You can also control this using auto_gloals_jit in php.ini: http://www.php.net/manual/en/ini.core.php#ini.auto-globals-jit
-------------------------------------

http://www.php.net/manual/en/language.variables.variable.php
-------------------------------------
Warning
Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.
-------------------------------------
To Top