"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}
O arquivo de configuração
O arquivo de configuração (php.ini) é lido quando o PHP inicia. Para as versões de módulo de servidor, isso acontece apenas quando o servidor web for inicializado. Para as versões CGI e CLI, isso acontece à cada invocação.
php.ini é procurado nesses lugares (na ordem):
-
Local específico do módulo SAPI (diretiva PHPIniDir no Apache 2, -c opção de linha de comando quando CGI e CLI, parâmetro php_ini no NSAPI, variável de ambiente PHP_INI_PATH no THTTPD)
-
A variável de ambiênte PHPRC. Antes do PHP 5.2.0 isto era conferido depois da chave de registro mencionada abaixo.
-
A partir do PHP 5.2.0, a localização do arquivo php.ini pode ser definida para versões diferentes do PHP. As seguintes chaves do registro são examinadas na ordem: [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z], [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] e [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x], aonde x, y e z significam a versão maior, menor e release do PHP. Se houver um valor para IniFilePath nestas chaves, então o primeiro encontrado será utilizado para a localização do php.ini (apenas Windows).
-
HKEY_LOCAL_MACHINE\SOFTWARE\PHP\IniFilePath (Local no registro do Windows)
-
Diretório de trabalho atual (exceto CLI)
-
O diretório do servidor web (para módulo SAPI), ou diretório do PHP (caso contrário, no Windows)
-
Diretório do Windows (C:\windows ou C:\winnt) (para o Windows), ou ou a opção de tempo de compilação --with-config-file-path
Se php-SAPI.ini existe (onde SAPI é o SAPI usado, então o nome de arquivo é, por exemplo, php-cli.ini ou php-apache.ini), é usado ao invés do arquivo php.ini. nome SAPI pode ser determinado pela função php_sapi_name().
Nota:
O servidor web Apache muda o diretório para raiz durante a inicialização, causando com que o PHP tente ler o arquivo php.ini da raiz do arquivo de sistema, se ele existir.
As diretivas do arquivo php.ini tratadas por extensões são documentadas respectivamente nas páginas das próprias extensões. A lista de diretivas principais está disponível no apêdice. No entanto, provavelmente nem todas as diretivas do PHP estão documentadas no manual. Para uma lista completa das diretivas disponíveis na sua versão do PHP, por favor leia seu arquivo php.ini. Também há alternativa de baixar a » última versão do arquivo php.ini dos repositórios do CVS, que pode ser de ajuda também.
Exemplo #1 php.ini example
; 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"
A partir do PHP 5.1.0, é possível acessar as variáveis .ini dentro dos arquivos .ini. Exemplo:open_basedir = ${open_basedir} ":/new/dir".
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.
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!
