大概是在命令行中按以下方式传递参数:
php /path/to/wwwpublic/path/to/script.php arg1 arg2
…然后通过脚本访问它们:
<?php// $argv[0] is '/path/to/wwwpublic/path/to/script.php'$argument1 = $argv[1];$argument2 = $argv[2];?>
通过HTTP传递参数(通过Web访问脚本)时,您需要做的是使用查询字符串并通过$ _GET超全局变量访问它们:
转到http://yourdomain.com/path/to/script.php?argument1=arg1&argument2=arg2
…和访问:
<?php$argument1 = $_GET['argument1'];$argument2 = $_GET['argument2'];?>
如果您希望脚本无论在哪里(从命令行或从浏览器)调用,都可以运行,您将需要以下内容:
编辑:
正如克苏鲁(Cthulhu)在评论中指出的那样,测试您在哪个环境中执行测试的最直接方法是使用PHP_SAPI常量。我已经相应地更新了代码:
<?phpif (PHP_SAPI === 'cli') { $argument1 = $argv[1]; $argument2 = $argv[2];}else { $argument1 = $_GET['argument1']; $argument2 = $_GET['argument2'];}?>


