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

4

1 回答 1

5

使用$argv从命令行传递参数。然后在脚本的开头相应地定义它们。

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

如果您将此代码放在脚本的开头,则它应该将 DEBUG_ON 定义为您从命令行作为参数传递的任何内容:php myscript.php arg1

您也可以define($argv[1], $argv[2]);然后使用php myscript.php DEBUG_ON 1将 DEBUG_ON 定义为 1。

于 2013-07-21T09:01:47.340 回答