Home >Backend Development >PHP Tutorial >PHP distinguishes testing and formal environments by setting system environment variables
1. Introduction
1. In PHP, you can use the getenv()
function and $_ENV
To obtain the environment variables
2. The environment variables exist in the system and do not change when the code is submitted
3. If the company has both a test server and a formal server Taiwan, it is a good way to distinguish the test environment and the formal environment through environment variables
4. Safe, don’t worry about submission coverage
2. Setting method
1. Open the etc/profile
file: vim ~/etc/profile
Add an environment variableexport PHP_ENV="test "
, the official server is set to ="prod"
so that some different operations can be done through system environment variables, or there are different database account passwords
Reload after setting: source ~/etc/profile
2. Modify PHP’s php-fpm.conf
file and set the PHP environment variable
Add at the bottom: env[PHP_ENV]=$PHP_ENV
Restart the PHP service, service php-fpm restart
3. What if getenv('PHP_ENV') cannot get the value?
It may be because php.ini does not load the $_ENV
variable definition by default. If you check phpinfo()
at this time, you will find the environment variables we set. Modify for "no value"
vim /etc/php/php.ini
Modify: variables_order: variables_order="EGPCS"
Restart the PHP service
4. Check phpinfo(), or use the getenv() function to obtain the variable
If you cannot obtain it, you can use$ _ENV
; Because the getenv
function is no longer supported in version 7.
For more php related knowledge, please visit php tutorial!
The above is the detailed content of PHP distinguishes testing and formal environments by setting system environment variables. For more information, please follow other related articles on the PHP Chinese website!