Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Define variable in command line

OK, the question is simple though I can't find a real working solution.

I want to be able to define something while invoking a specific script.

I have tried it like php -d DEBUG_ON myscript.php but it's not working (when testing if (defined("DEBUG_ON")) { } inside the script, it returns false)

Also tried something along the lines of php -r ('define("DEBUG_ON",1);') myscript.php; which doesn't work either.

So, any ideas? (Or any suggestions on how I could achieve the very same effect?)

Upvotes: 0

Views: 1433

Answers (1)

Once Upon a Dev
Once Upon a Dev

Reputation: 1108

Use $argv to pass arguments from command line. Then define them accordingly in the beginning of the script.

if(php_sapi_name() === 'cli') {
    define('DEBUG_ON', $argv[1]);
}

If you put this code in the beginning of your script, it should then define DEBUG_ON to whatever you pass as argument from commandline: php myscript.php arg1

You can also define($argv[1], $argv[2]); and then use php myscript.php DEBUG_ON 1 to define DEBUG_ON as 1.

Upvotes: 5

Related Questions