B L
B L

Reputation: 197

Set php variable from bash command line

Just to make things easier for me to test while still in the development phase, I was wondering if there was a way that I can set a php variable from the command line before calling my script.

For example, I test out my php scripts by typing: php scriptname.php

And I look at the output. However, lets say that scriptname.php contains a variable $myVar that was never set.

Is there a way that I can set this on the command line?

Something like $myVar='foo' php scriptname.php ?

Upvotes: 1

Views: 955

Answers (1)

T0xicCode
T0xicCode

Reputation: 4951

You'll have to use the $_ENV superglobal to pickup environment variables:

$var = array_key_exist('env_key', $_ENV) ? $_ENV['env_key'] : 'default';

Then run your scripts as follow: env_key=foo php scriptname.php.

Upvotes: 2

Related Questions