CakeFest 2024: The Official CakePHP Conference

$_ENV

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

$_ENV環境変数

説明

環境変数として現在のスクリプトに渡された変数の連想配列です。

これらの変数は PHP パーサが実行されている環境から PHP のグローバル名前空間に取り込まれます。 その多くは、PHP が実行されているシェルに由来するものであり、 システムが違えばシェルも違ってくるため、確定的なリストを 得ることは不可能です。定義されている環境変数のリストについては 使用しているシェルのドキュメントをご覧ください。

CGI 変数を含むその他の環境変数も、 PHP がサーバーモジュールとして実行されているか CGI プロセッサとして 実行されているかに関わらずここに含まれます。

例1 $_ENV の例

<?php
echo 'My username is ' .$_ENV["USER"] . '!';
?>

"bjori" がこのスクリプトを実行したとします。

上の例の出力は、 たとえば以下のようになります。

My username is bjori!

注意

注意:

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

参考

add a note

User Contributed Notes 2 notes

up
166
gabe-php at mudbugmedia dot com
13 years ago
If your $_ENV array is mysteriously empty, but you still see the variables when calling getenv() or in your phpinfo(), check your http://us.php.net/manual/en/ini.core.php#ini.variables-order ini setting to ensure it includes "E" in the string.
up
5
aasasdasdf at yandex dot ru
9 years ago
Please note that writing to $_ENV does not actually set an environment variable, i.e. the variable will not propagate to any child processes you launch (except forked script processes, in which case it's just a variable in the script's memory). To set real environment variables, you must use putenv().

Basically, setting a variable in $_ENV does not have any meaning besides setting or overriding a script-wide global variable. Thus, one should never modify $_ENV except for testing purposes (and then be careful to use putenv() too, if appropriate).

PHP will not trigger any kind of error or notice when writing to $_ENV.
To Top