34

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.

3
  • 1
    is the space between -d and APPLICATION_ENV a typo? Try putting putenv("APPLICATION_ENV=staging"); in script.php
    – Tdelang
    Aug 7, 2013 at 8:13
  • Do you need to set some variables from within the script, or access an environment variable from within the script? If the latter, what code were you using to access it?
    – DaveRandom
    Aug 7, 2013 at 8:15
  • In the script variable is retrieved with getenv() function.
    – Wiktor
    Aug 7, 2013 at 8:25

7 Answers 7

46
APPLICATION_ENV=staging php script.php

The variable will be available in the $_SERVER array:

echo $_SERVER['APPLICATION_ENV'];
1
  • As of Oct 2021, PHP 7.4, this throws a warning. Oct 3, 2021 at 14:48
19

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).

1
  • This should be the selected answer as a wrapper script requires your deployment administrator to perform the same amount of effort and use the same amount of variables but offers you far more control over what is running instead of hoping that the environment variables work across the unknown systems that your software may be deployed to.
    – Matthew
    Feb 24, 2021 at 8:22
14

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'

or directly for each script's call:

export APPLICATION_ENV='staging' && php my-script.php
1
  • I know, I just don't want to alter the script anyhow.
    – Wiktor
    Aug 7, 2013 at 8:24
12

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 
6

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.

2
  • +1 For "You may need to relog [in] for this change to take effect". I've been beating my head against a wall wondering why stuff in my /etc/environment didn't work, until I saw this comment. May 4, 2017 at 15:13
  • Actually had an interesting situation where /etc/environment was not being loaded. I had to add source /etc/environment to my ~/.bashrc manually
    – thorne51
    May 11, 2018 at 7:53
2

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]);
}
1

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.