略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: $GLOBALS

2024-12-21

$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 noteadd a note

User Contributed Notes 4 notes

up
29
mstraczkowski at gmail dot com
9 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
30
therandshow at gmail dot com
10 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
vittorio.zamparella at famous googlemail
5 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.
-------------------------------------
up
-57
stevenjeffries at gmail dot com
6 years 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.php";
}
test();

?>

Outputs:

Foo is in global scope.
Foo is NOT in global scope.

官方地址:https://www.php.net/manual/en/reserved.variables.globals.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3