Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting PHP enviromental variable while running command line script

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.

like image 228
Wiktor Avatar asked Aug 07 '13 08:08

Wiktor


People also ask

How do you set an environment variable in a script?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.

How can you set environment variables from the command line?

To set (or change) a environment variable, use command " set varname=value ". There shall be no spaces before and after the '=' sign. To unset an environment variable, use " set varname= ", i.e., set it to an empty string.

Can we use PHP for command line scripts?

As of version 4.3. 0, PHP supports a new SAPI type (Server Application Programming Interface) named CLI which means Command Line Interface. As the name implies, this SAPI type main focus is on developing shell (or desktop as well) applications with PHP.


2 Answers

APPLICATION_ENV=staging php script.php 

The variable will be available in the $_SERVER array:

echo $_SERVER['APPLICATION_ENV']; 
like image 140
Marek Avatar answered Sep 22 '22 15:09

Marek


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

like image 31
DaveRandom Avatar answered Sep 23 '22 15:09

DaveRandom