1

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 ?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.