"Since PHP 5.1.0, it is possible to refer to existing .ini variables from within .ini files."
If you have several configurations that you switch between (say development/testing/staging), or there is some other reason why several settings scattered through the .ini file might need to be changed all together on occasion, then combining this with a custom block can bring all the bits that need changing into one place:
[Customization]
custom.mode = "development"
custom.display_errors = "on"
custom.error_reporting = 30719
[PHP]
;;;;;;;;;;;;;;;;;;;
; About php.ini ;
....
And then refer to these variables in the rest of the file:
custom.session.save_path = "/tmp/"${custom.mode}
Bringing all the changes into one location in the file is often of immense benefit.
---
Unfortunately, variable names cannot (yet) be nested. Otherwise one could have one .ini file with several customisation blocks, and a single variable to choose which set of variables to use:
[Customization]
custom.mode = "development"
[Customization Development]
custom.development.display_errors = on
[Customization Production]
custom.development.display_errors = off
...
display_errors = ${custom.${custom.mode}.display_errors}
設定ファイル
設定ファイル (php.ini) は PHP の起動時に読み込まれます。 PHP のサーバモジュール版では、Web サーバの起動時に 一度だけ読み込まれます。CGI 版と CLI 版では、スクリプトが呼び出される度に読み込まれます。
読み込む php.ini は、これらの場所を順に探します。
- SAPI モジュール特有の場所 (Apache 2 における PHPIniDir ディレクティブ、 CGI/CLI 版における -c コマンドラインオプション、 NSAPI における php_ini パラメータ、 THTTPD における PHP_INI_PATH 環境変数)
- PHPRC 環境変数。PHP 5.2.0 より前では、 これは、次に挙げるレジストリキーの後にチェックされていました。
- PHP 5.2.0 以降では、PHP のバージョンごとに別の場所の php.ini を使用することができます。以下のレジストリキーを順に調べて使用します。 [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z]、 [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] および [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x]。 x、y および z はそれぞれ PHP のメジャーバージョン、マイナーバージョン、 リリース番号を表します。これらのキーに IniFilePath の値が設定されていれば、最初に見つかった場所の php.ini を使用します (Windows のみ)。
- [HKEY_LOCAL_MACHINE\SOFTWARE\PHP] にある IniFilePath の値 (Windows のみ)。
-
現在の作業ディレクトリ (CLI を除く)
- Web サーバのディレクトリ (SAPI モジュールの場合)、 もしくは PHP ディレクトリ (そうでない Windows の場合)
- Windows ディレクトリ (C:\windows もしくは C:\winnt)) (Windows の場合) 、もしくはコンパイル時のオプション --with-config-file-path
php-SAPI.ini (ここで SAPI は使用する SAPI 名。 たとえば php-cli.ini や php-apache.ini) が存在する場合、 php.ini の代わりに使用されます。 SAPI 名は php_sapi_name() によって決定されます。
注意:
Apache web サーバは、スタート時にディレクトリをルート に変更するので、ファイルシステムのルートに php.ini が存在する場合、PHP はそれを読もうとします。
拡張モジュールに対する php.ini ディレクティブは、 各拡張モジュールのドキュメントで解説されています。 コア ディレクティブ (PHP 本体に対するディレクティブ)のリストは付録にまとめられています。 ただし、(更新の都合上)すべての PHP ディレクティブが 本マニュアル中で解説されている訳ではありません。 使っているバージョンの PHP で指定可能なすべてのディレクティブについては、 php.ini ファイル内に詳細なコメントが記されていますので、参照してください。 もしくは、SVN から入手可能な » 最新の php.ini も有用でしょう。
例1 php.ini の例
; 引用符をつけないセミコロン(;)の後のテキストは、すべて無視されます [php] ; セクションマーカ (角括弧の中のテキスト) は無視されます ; 論理値は、次のいずれかで指定します ; true, on, yes ; または false, off, no, none register_globals = off magic_quotes_gpc = yes ; 文字列を二重引用符で括ることも可能です include_path = ".:/usr/local/lib/php" ; バックスラッシュは他の文字と同様に処理されます include_path = ".;c:\php\lib"
PHP 5.1.0 以降、ini ファイル内で既存の ini 変数を参照することが可能です。 例: open_basedir = ${open_basedir} ":/new/dir"
Something to note which is not well documented is that when you are specifying the path, it is JUST the path that is needed - not the path and filename. In the registry locations, you need to just put the folder path (e.g. C:\PHP\) and not the full path to the INI file (e.g. C:\PHP\php.ini). This will particularly save you some headaches if you are trying to run multiple versions of PHP on one server!
