How to distinguish test environment from formal environment by setting system environment variables in PHP
This article mainly introduces how to distinguish between the test environment and the formal environment by setting system environment variables in PHP, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
First, introduction
Environment variables can be obtained in 1.PHP through the getenv () function and $_ ENV.
two。 Environment variables are stored in the system and do not change with the submission of the code
3. If the company has two test servers and formal servers, it is a good way to distinguish the test environment from the formal environment by means of environment variables.
4. Safe, don't worry about submission overwrite
Second, setting method
1. Open the etc/profile file: vim ~ / etc/profile
Add an environment variable export PHP_ENV= "test", and set the official server to = "prod"
In this way, you can do some different operations through the system environment variables, or there may be different database account passwords.
Reload after setting: source ~ / etc/profile
two。 Modify the php-fpm.conf file of PHP 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') can't get the value?
It may be because php.ini does not load the $_ ENV variable definition by default. If you look at phpinfo () at this time, you will find that the environment variable we set is "no value"
Modify vim / etc/php/php.ini
Modified: variables_order:variables_order= "EGPCS"
Restart the PHP service
4. Check phpinfo (), or use the getenv () function to get variables
If it is not available, use $_ ENV; because the getenv function is no longer supported in version 7.
Thank you for reading this article carefully. I hope the article "how to distinguish the test environment from the formal environment in PHP by setting system environment variables" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!