0

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

1 Answer 1

5

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.

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

3 Comments

Arghh... The site is based on CodeIgniter, running both locally and on a live server, and also making use of terminal processes a lot, so I struggled to avoid playing with $argv not to mess things up; though it seems like I can't avoid it for much longer...
That last edit on your original answer seems like a neat little trick! ;-)
That should prevent it from interfering with normal use.

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.