Dr.*_*eon 0 php command-line-interface
好的,问题很简单,但我找不到真正有效的解决方案.
我想define在调用特定脚本时能够做些什么.
我试过它php -d DEBUG_ON myscript.php但它不起作用(当if (defined("DEBUG_ON")) { }在脚本内部测试时,它会返回false)
还尝试了一些事情php -r ('define("DEBUG_ON",1);') myscript.php; 这也不起作用.
那么,有什么想法吗?(或者有关如何达到同样效果的任何建议?)
使用$ argv从命令行传递参数.然后在脚本的开头相应地定义它们.
if(php_sapi_name() === 'cli') {
define('DEBUG_ON', $argv[1]);
}
Run Code Online (Sandbox Code Playgroud)
如果您将此代码放在脚本的开头,那么它应该将DEBUG_ON定义为从命令行传递的任何内容: php myscript.php arg1
您也可以define($argv[1], $argv[2]);使用php myscript.php DEBUG_ON 1将DEBUG_ON定义为1.