PHP - Windows 中的命令行参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/65173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:21:46  来源:igfitidea点击:

PHP - command line arguments in Windows

phpcommand-lineparameters

提问by

I'm trying to run PHP from the command line under Windows XP.

我正在尝试在 Windows XP 下从命令行运行 PHP。

That works, except for the fact that I am not able to provide parameters to my PHP script.

这是有效的,除了我无法为我的 PHP 脚本提供参数的事实。

My test case:

我的测试用例:

echo "param = ".$param."\n";  
var_dump($argv);  

I want to call this as:

我想称之为:

php.exe -f test.php -- param=test

php.exe -f test.php -- param=test

But I never get the script to accept my parameter.

但是我从来没有让脚本接受我的参数。

The result I get from the above script

我从上面的脚本中得到的结果

`PHP Notice: Undefined variable: param in C:\test.php on line 2

`PHP 注意:未定义变量:第 2 行 C:\test.php 中的参数

param = ''
array(2) {
  [0]=> string(8) "test.php"
  [1]=> string(10) "param=test"
}

I am trying this using PHP 5.2.6. Is this a bug in PHP5?

我正在尝试使用 PHP 5.2.6。这是PHP5中的错误吗?

The parameter passing is handled in the online helpNote: If you need to pass arguments to your scripts you need to pass -- as the first argument when using the -f switch.This seemed to be working under PHP4, but not under PHP5. Under PHP4 I could use the same script that could run on the server without alteration on the command line. This is handy for local debugging, for example saving the output in a file to be studied.

参数传递在联机帮助中处理Note: If you need to pass arguments to your scripts you need to pass -- as the first argument when using the -f switch.这似乎在 PHP4 下有效,但在 PHP5 下无效。在 PHP4 下,我可以使用可以在服务器上运行的相同脚本,而无需在命令行上进行更改。这对于本地调试很方便,例如将输出保存在要研究的文件中。

回答by Adam Wright

Why do you have any expectation that param will be set to the value? You're responsible for parsing the command line in the fashion you desire, from the $argv array.

为什么你期望 param 会被设置为值?您负责以您想要的方式从 $argv 数组解析命令行。

回答by Gerrit

If you want to pass the params similar to GET vars, then you can use the parse_str()function. Something similar to this:

如果你想传递类似于 GET vars 的参数,那么你可以使用parse_str()函数。类似的东西:

<?php
parse_str($argv[1]);
?>

Would produce a variable of $test with a value of myValue.

将产生一个值为 myValue 的 $test 变量。

Hope this helps!

希望这可以帮助!

回答by Mark Biek

The parameter passing is handled in the 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. This seemed to be working under PHP4, but not under PHP5.

参数传递在联机帮助中处理 注意:如果需要将参数传递给脚本,则需要传递 -- 作为使用 -f 开关时的第一个参数。这似乎在 PHP4 下有效,但在 PHP5 下无效。

But PHP still doesn't parse those arguments. It just passes them to the script in the $argv array.

但是 PHP 仍然不解析这些参数。它只是将它们传递给 $argv 数组中的脚本。

The only reason for the --is so that PHP can tell which arguments are meant for the PHP executable and which arguments are meant for your script.

对于唯一的原因-是为了让PHP可以知道哪些参数是意味着PHP可执行文件和参数都意味着你的脚本。

That lets you do things like this:

这让你可以做这样的事情:

php -e -n -f myScript.php -- -f -n -e

(The -f, -n, & -e afterthe -- are passed to myScript.php. The ones before are passed to PHP itself).

(-f, -n, & -e--之后传递给 myScript.php。之前的传递给 PHP 本身)。

回答by Jeremy Weathers

PHP does not parameterize your command line parameters for you. See the output where your 2nd entry in ARGV is "param=test".

PHP 不会为您参数化您的命令行参数。查看 ARGV 中的第二个条目是“param=test”的输出。

What you most likely want is to use the PEAR package http://pear.php.net/package/Console_CommandLine: "A full featured command line options and arguments parser".

您最可能想要的是使用 PEAR 包http://pear.php.net/package/Console_CommandLine:“功能齐全的命令行选项和参数解析器”。

Or you can be masochistic and add code to go through your ARGV and set the parameters yourself. Here's a very simplistic snippet to get you started (this won't work if the first part isn't a valid variable name or there is more than 1 '=' in an ARGV part:

或者您可以自虐并添加代码以通过您的 ARGV 并自己设置参数。这是一个非常简单的片段,可以帮助您入门(如果第一部分不是有效的变量名称或 ARGV 部分中的“=”超过 1 个,则这将不起作用:

foreach($argv as $v) {
    if(false !== strpos($v, '=')) {
        $parts = explode('=', $v);
        ${$parts[0]} = $parts[1];
    }
}

回答by Derek Kurth

You can do something like:

您可以执行以下操作:

if($argc > 1){
    if($argv[1] == 'param=test'){
        $param = 'test';
    }
}

Of course, you can get much more complicated than that as needed.

当然,您可以根据需要变得比这更复杂。

回答by Ben

$argv is an array containing all your commandline parameters... You need to parse that array and set $param yourself.

$argv 是一个包含所有命令行参数的数组...您需要解析该数组并自己设置 $param。

$tmp = $argv[1];             // $tmp="param=test"
$tmp = explode("=", $tmp);   // $tmp=Array( 0 => param, 1 => test)

$param = $tmp[1];            // $param = "test";

回答by rcphq

You could use something like

你可以使用类似的东西

if (isset($argv[1]) {
 $arg1 = $argv[1];             
 $arg1 = explode("=", $arg1);   
 $param = $arg1[1];            
}

(how to handle the lack of parameter/s is up to you) or if you need a more complex scenario, look into a commandline parser library such as the one from Pear.

(如何处理缺少参数取决于您)或者如果您需要更复杂的场景,请查看命令行解析器库,例如Pear 中的解析器库。

using the ${$parts[0]} = $parts[1];posted in another solution lets you override any variable in your code, which doesnt really sound safe.

使用${$parts[0]} = $parts[1];在另一个解决方案中发布的内容可以让您覆盖代码中的任何变量,这听起来并不安全

回答by kander

If you like living on the cutting edge, PHP 5.3 has the getOpt()command which will take care of all this messy business for you. Somewhat.

如果您喜欢生活在最前沿,PHP 5.3 有getOpt()命令,它将为您处理所有这些杂乱的事务。有些。

回答by M.Hefny

command line example:

命令行示例:

php myserver.php host=192.168.1.4 port=9000

php myserver.php 主机=192.168.1.4 端口=9000

in myserver.php use the following lines:

在 myserver.php 中使用以下几行:

<?php

parse_str(implode('&', array_slice($argv, 1)), $_GET);
// Read Arguments
if (array_key_exists('host',$_GET))
{
    $host = $_GET['host'];
}
if (array_key_exists('port',$_GET))
{
    $port = $_GET['port'];
}
?>