Home > Backend Development > PHP Tutorial > PHP distinguishes testing and formal environments by setting system environment variables

PHP distinguishes testing and formal environments by setting system environment variables

藏色散人
Release: 2023-04-08 11:40:01
forward
2471 people have browsed it

PHP distinguishes testing and formal environments by setting system environment variables

1. Introduction

1. In PHP, you can use the getenv() function and $_ENVTo 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!

Related labels:
php
source:oschina.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template