$GLOBALS

(PHP 4, PHP 5, PHP 7, PHP 8)

$GLOBALSグローバルスコープで使用可能なすべての変数への参照

説明

スクリプトのグローバルスコープに現在定義されているすべての変数への参照を含む連想配列です。 変数名が配列のキーとなります。

例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 全体に対するあらゆる書き込み/読み書き操作も同様です

?>

注意

注意:

これは 'スーパーグローバル' あるいは自動グローバル変数と呼ばれるものです。 スクリプト全体を通してすべてのスコープで使用することができます。 関数やメソッドの内部で使用する場合にも global $variable; とする必要はありません。

注意: 変数の可用性

他のスーパーグローバル とは異なり、$GLOBALS は PHP で常に使用可能です。

注意:

PHP 8.1.0 以降、$GLOBALS はグローバルな シンボルテーブル の、読み取り専用のコピーになりました。つまり、グローバル変数は $GLOBALS のコピーを通じて書き換えられなくなったということです。 それより前のバージョンでは、 $GLOBALS 配列は値渡しの振る舞いの例外とされ、そのコピーを通じてグローバル変数の書き換えができていました。

<?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 配列のコピーを走査し、個別のキーと値を $GLOBALS 配列に書き戻すようにして下さい。
foreach ($globals as $key => $value) {
$GLOBALS[$key] = $value;
}

?>

add a note

User Contributed Notes 4 notes

up
20
inafeeur at gmail dot com
2 years 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
14 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
12 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 $GLOBALSYou 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
8 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-------------------------------------WarningPlease 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