Setting PHP enviromental variable while running command line script

47,458

Solution 1

APPLICATION_ENV=staging php script.php

The variable will be available in the $_SERVER array:

echo $_SERVER['APPLICATION_ENV'];

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

Solution 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'

Solution 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 

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

Share:
47,458
Wiktor
Author by

Wiktor

Entrepreneur and programmer.

Updated on July 09, 2022

Comments

  • Wiktor
    Wiktor almost 2 years

    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.