我正在尝试从 Windows XP 下的命令行运行 PHP .
这行得通,除了我无法为我的 PHP 脚本提供参数这一事实。
我的测试用例:
echo "param = " . $param . "\n";
var_dump($argv);
我想称它为:
php.exe -f test.php -- param=test
但我从来没有让脚本接受我的参数。
我从上面的脚本得到的结果:
PHP Notice: Undefined variable: param in C:\test.php on line 2
param = ''
array(2) {
[0]=> string(8) "test.php"
[1]=> string(10) "param=test"
}
我正在尝试使用 PHP 5.2.6。这是 PHP 5 中的错误吗?
参数传递在online help中处理:
Note: If you need to pass arguments to your scripts you need to pass -- as the first argument when using the -f switch.
这似乎在 PHP 4 下有效,但在 PHP 5 下无效。
在 PHP 4 下,我可以使用可以在服务器上运行的相同脚本,而无需在命令行上进行更改。这个方便本地调试,比如把输出保存到文件中,有待研究。
最佳答案
为什么您期望 param 会被设置为该值?
您负责以您希望的方式从 $argv 数组中解析命令行。
关于PHP - Windows 中的命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65173/