LoginSignup
8
9

More than 5 years have passed since last update.

$GLOBALSに_SERVERや_ENVが登録される条件

Last updated at Posted at 2014-12-08

$GLOBALS

$GLOBALS — グローバルスコープで使用可能なすべての変数への参照
http://php.net/manual/ja/reserved.variables.globals.php

この$GLOBALS変数のキーに_SERVER_ENVが登録される時とされない時があります。

auto_globals_jit

これはphp.iniのauto_globals_jitの設定で決定されます。

スクリプト

print_r(array_keys($GLOBALS));

auto_globals_jit=Onの結果

Array
(
    [0] => _GET
    [1] => _POST
    [2] => _COOKIE
    [3] => _FILES
    [4] => GLOBALS
)

auto_globals_jit=Offの結果

Array
(
    [0] => _GET
    [1] => _POST
    [2] => _COOKIE
    [3] => _SERVER
    [4] => _ENV
    [5] => _REQUEST
    [6] => _FILES
    [7] => GLOBALS
)

auto_globals_jit=Onでも$_SERVERに一度でもアクセスすると登録されます。

$_SERVER; // 値を利用しなくてもよい
print_r(array_keys($GLOBALS));
Array
(
    [0] => _GET
    [1] => _POST
    [2] => _COOKIE
    [3] => _FILES
    [4] => _SERVER
    [5] => GLOBALS
)

まとめ

  • php.ini.の設定に関わらず$GLOBALS['_SERVER']が存在する事を前提とするなら$GLOBALS使用の前に$_SERVERにアクセスする。
$_SERVER;
$method = $GLOBALS['_SERVER']['REQUEST_METHOD'];
  • パフォーマンス向上のためにはauto_globals_jit=Onにする
  • ライブラリ開発時の設定はauto_globals_jit=On
  • $_REQUEST対応はPHP5.3以降、その他はPHP5.0以降対応の模様 (ドキュメントにないとの指摘)

参照

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9