Setting PHP enviromental variable while running co

2019-02-02 06:57发布

问题:

I need to run a PHP script from command line and I need to set some environmental variables. Unfortunately, following does not work:

php -dAPPLICATION_ENV=staging script.php

What I'd like to accomplish is having APPLICATION_ENV variable set.

回答1:

APPLICATION_ENV=staging php script.php

The variable will be available in the $_SERVER array:

echo $_SERVER['APPLICATION_ENV'];


回答2:

There is no way to set environment variables from the command line specifically for the execution of a script by passing options to the PHP binary.

You have a few options:

  1. Set the variable globally on the system.
  2. Set the variable on the command line before calling the script. This will persist in the environment after your script has finished executing, which you may not want.
  3. Wrap the PHP script in another script, allowing you to create a temporary variable that exists only for the duration of the script.
  4. Use a command line option instead of an environment variable.

The last two options are probably the cleanest way to do this, in that the variable created only exists for the run time of your script.

The implementation of option 1 is system dependent.

The implementation of option 2 is also system dependent - on Windows you would do set APPLICATION_ENV=staging&& php script.php and on *nix it would be export APPLICATION_ENV='staging' && php script.php.

If you were to go for option 3 you might be tempted to go for a shell script, but this is not portable (you would need a batch file for Windows and a shell script for *nix environments. Instead, I'd suggest you write a simple PHP wrapper script, something like this:

<?php

    putenv('APPLICATION_ENV=staging');

    include('script.php');

This allows you to leave your target script unchanged and set the environment variable for the script's session only.

A more complex wrapper script could easily be created which would allow you to specify variables on the command line, and even dynamically specify the script which should be executed when these variables are set.

Option 4 can be implemented using the $argv variable:

<?php

    $applicationEnv = $argv[1];

    // rest of you script

...and call the script like:

php script.php staging

However, it occurs to me that you seem to be indicating to the script which environment is running in (staging, dev, live, etc) - in which case it might be simplest to set a server-wide variable, and rename it as necessary to prevent collision with variables that other applications may be setting. That way you can simply invoke the script and not need to worry about this. This is assuming that you staging environment runs on a different machine to the live (which it should be).



回答3:

When you execute a PHP script from the command line, it inherits the environment variables defined in your shell. That means you can set an environment variable using the export command like so:

export APPLICATION_ENV='staging'


回答4:

Here is an example for setting one envirnnomental variable :

ENV_VAR='var' php script.php 

Just in case you want to set multiple variables Try this :

ENV_VAR1=1 ENV_VAR2=2 ENV_VAR3=3 php script.php 


回答5:

You can set a variable in /etc/environment like FOO="bar" which is then accessible with getenv() in both CLI and web requests. You may need to relog for this change to take effect.



回答6:

Try using putenv and pass the variables through parameters

php script.php APPLICATION_ENV=staging

And in the script.php code:

for($i=1;$i<count($argv);$i++){
     putenv($argv[$i]);
}


回答7:

I have the same situation and i use next code (it works for me):

export APPLICATION_ENV=staging && php script.php

Hope it will helpful for you too.