运行时配置
在线手册:中文 英文
PHP手册

配置文件

配置文件(PHP 3 中是 php3.ini,自 PHP 4 起是 php.ini)在 PHP 启动时被读取。对于服务器模块版本的 PHP,仅在 web 服务器启动时读取一次。对于 CGICLI 版本,每次调用都会读取。

php.ini 的搜索路径如下(按顺序):

如果存在 php-SAPI.ini(SAPI 是当前所用的 SAPI 名称,因此实际文件名为 php-cli.iniphp-apache.ini 等),则会用它替代 php.ini。SAPI 的名称可以用 php_sapi_name() 来测定。

Note:

Apache web 服务器在启动时会把目录转到根目录,这将导致 PHP 尝试在根目录下读取 php.ini,如果存在的话。

由扩展库处理的 php.ini 指令,其文档分别在各扩展库的页面。内核配置选项见附录。不过也许不是所有的 PHP 指令都在手册中有文档说明。要得到自己的 PHP 版本中的配置指令完整列表,请阅读 php.ini 文件,其中都有注释。此外,也许从 SVN 得到的» 最新版 php.ini 也有帮助。

Example #1 php.ini 例子

; any text on a line after an unquoted semicolon (;) is ignored
[php] ; section markers (text within square brackets) are also ignored
; Boolean values can be set to either:
;    true, on, yes
; or false, off, no, none
register_globals = off
track_errors = yes

; you can enclose strings in double-quotes
include_path = ".:/usr/local/lib/php"

; backslashes are treated the same as any other character
include_path = ".;c:\php\lib"

自 PHP 5.1.0 起,有可能在 .ini 文件内引用已存在的 .ini 变量。例如:open_basedir = ${open_basedir} ":/new/dir"


运行时配置
在线手册:中文 英文
PHP手册
PHP手册 - N: 配置文件

用户评论:

Hayley Watson (08-May-2010 11:53)

"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}

prjorgen at gmail dot com (06-Apr-2009 09:42)

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!