PHP-CLI setting and reading of environment variables

Source: Internet
Author: User

Http://luokr.com/p/30

Usually when we maintain the PHP line project, in order to isolate the configuration and code, we will use fastcgi_param the form to define the environment variables in the Nginx configuration file (Apache can use SetEnv instructions). This allows you to use the getenv function to get the values of the environment variables during the PHP-FPM run.

So how do we set the environment variables for PHP-CLI? The practice is also simple.

Execute directly on the terminal:

    1. $ export art_env=production

You can naturally get the value of the environment variable when you follow the direct use of the PHP-CLI command ART_ENV :

    1. $ php -R "Var_dump (getenv (' art_env '));"
    2. String(ten) "Production"

But this is usually the case: the user we are currently logged on is not the right one to run the Php-cli script, such as we expect to use the www-data user to run the Php-cli script, which we usually do:

    1. $ sudo -u www-data php -R "Var_dump (getenv (' art_env '));"
    2. BOOL(false)

You will find that you cannot get to the environment variable. Check sudo --help to see that we also need to set -E parameters:

    1. $ sudo -- Help
    2. ...
    3. -E, --Preserve-env Preserve user environment when running command /c11>

Follow the documentation to fill in the instructions:

    1. $ sudo -E -u www-data php -R "Var_dump (getenv (' art_env '));"
    2. String(ten) "Production"

Or, more directly, specify the value of the environment variable directly:

    1. $ sudo -u www-data art_env=testing php -R "Var_dump (getenv (' art_env '));"
    2. String(7) "testing"

It is important to note that the PHP code here must use the GETENV function to get the environment variables and not rely solely on global variables $_ENV . Global variables are $_ENV not always available unless the value explicitly set in the php.ini file variables_order contains E, for example variables_order = "EGPCS" . For more details, refer to the Global Variables section of the PHP documentation.

PHP-CLI setting and reading of environment variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.