配置文件(php.ini)在 PHP 启动时被读取。对于服务器模块版本的 PHP,仅在 web 服务器启动时读取一次。对于 CGI 和 CLI 版本,每次调用都会读取。
php.ini 的搜索路径如下(按顺序):
如果存在 php-SAPI.ini(SAPI 是当前所用的 SAPI 名称,因此实际文件名为 php-cli.ini 或 php-apache.ini 等),则会用它替代 php.ini。SAPI 的名称可以用 php_sapi_name() 来测定。
Note:
Apache web 服务器在启动时会把目录转到根目录,这将导致 PHP 尝试在根目录下读取 php.ini,如果存在的话。
Note:
在 php.ini 中可以使用环境变量。
由扩展库处理的 php.ini 指令,其文档分别在各扩展库的页面。内核配置选项见附录。不过也许不是所有的 PHP 指令都在手册中有文档说明。要得到自己的 PHP 版本中的配置指令完整列表,请阅读 php.ini 文件,其中都有注释。此外,也许从 Git 得到的» 最新版 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"。
Greg Robson (2013-06-04 11:18:47)
If you are on Windows and the
php --ini
command is showing the path you do not want, check the PATH environment variable.
The command line was looking for php.ini in the folder where it found php.exe. In my case this meant it was looking in
c:\Program Files (x86)\php\5.2.6
and not
c:\PHP\5.4.7
Might easily be overlooked when adding new versions to your computer.
Hayley Watson (2010-05-08 15:53:41)
"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 (2009-04-06 13:42:41)
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!