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
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 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. 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.
参数传递在在线帮助中处理注意:如果需要将参数传递给脚本,则需要传递 - 作为使用-f开关时的第一个参数。这似乎是在PHP4下工作,但不是在PHP5下。在PHP4下,我可以使用可以在服务器上运行的相同脚本,而无需在命令行上进行更改。这对于本地调试很方便,例如将输出保存在要研究的文件中。
11 个解决方案
#1
7
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数组中以您想要的方式解析命令行。
#2
3
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 after the -- are passed to myScript.php. The ones before are passed to PHP itself).
(-f之后的-f,-n,&-e传递给myScript.php。之前的那些传递给PHP本身)。
#3
3
If you want to pass the params similar to GET vars, then you can use the parse_str() function. Something similar to this:
如果你想传递类似于GET变量的参数,那么你可以使用parse_str()函数。与此类似的东西:
<?php
parse_str($argv[1]);
?>
Would produce a variable of $test with a value of myValue.
会产生$ test的变量,其值为myValue。
Hope this helps!
希望这可以帮助!
#4
2
You can use the getopt();
你可以使用getopt();
Check this::
http://sharingphp.blogspot.com/2011/04/php-cli-script-and-command-line.html
#5
1
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];
}
}
#6
0
$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";
#7
0
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.
当然,根据需要,你可以得到比这更复杂的东西。
#8
0
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.
(如何处理缺少参数/ s取决于您)或者如果您需要更复杂的场景,请查看命令行解析器库,例如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];在另一个解决方案中发布允许您覆盖代码中的任何变量,这听起来并不安全。
#9
0
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()命令,它将为您处理所有这些混乱的业务。有些。
#10
0
command line example:
命令行示例:
php myserver.php host=192.168.1.4 port=9000
php myserver.php host = 192.168.1.4 port = 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'];
}
?>
#11
-1
you can use the $argv array. like this:
你可以使用$ argv数组。像这样:
<?php
echo $argv[1];
?>
remember that the first member of the $argv array (which is $argv[0]) is the name of the script itself, so in order to use the parameters for the application, you should start using members of the $argv[] from the '1'th index. when calling the application, use this syntax:
请记住,$ argv数组的第一个成员($ argv [0])是脚本本身的名称,因此为了使用应用程序的参数,您应该开始使用$ argv []的成员。 '1'指数。在调用应用程序时,请使用以下语法:
php myscript.php -- myValue
there is no need to put a name for the parameter. as you saw, what you called the var_dump() on the $argv[], the second member (which is the first parameter) was the string PARAM=TEST. right? so there is no need to put a name for the param. just enter the param value.
没有必要为参数添加名称。如你所见,你在$ argv []上调用var_dump(),第二个成员(第一个参数)是字符串PARAM = TEST。对?所以没有必要为param命名。只需输入参数值。
#1
7
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数组中以您想要的方式解析命令行。
#2
3
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 after the -- are passed to myScript.php. The ones before are passed to PHP itself).
(-f之后的-f,-n,&-e传递给myScript.php。之前的那些传递给PHP本身)。
#3
3
If you want to pass the params similar to GET vars, then you can use the parse_str() function. Something similar to this:
如果你想传递类似于GET变量的参数,那么你可以使用parse_str()函数。与此类似的东西:
<?php
parse_str($argv[1]);
?>
Would produce a variable of $test with a value of myValue.
会产生$ test的变量,其值为myValue。
Hope this helps!
希望这可以帮助!
#4
2
You can use the getopt();
你可以使用getopt();
Check this::
http://sharingphp.blogspot.com/2011/04/php-cli-script-and-command-line.html
#5
1
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];
}
}
#6
0
$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";
#7
0
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.
当然,根据需要,你可以得到比这更复杂的东西。
#8
0
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.
(如何处理缺少参数/ s取决于您)或者如果您需要更复杂的场景,请查看命令行解析器库,例如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];在另一个解决方案中发布允许您覆盖代码中的任何变量,这听起来并不安全。
#9
0
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()命令,它将为您处理所有这些混乱的业务。有些。
#10
0
command line example:
命令行示例:
php myserver.php host=192.168.1.4 port=9000
php myserver.php host = 192.168.1.4 port = 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'];
}
?>
#11
-1
you can use the $argv array. like this:
你可以使用$ argv数组。像这样:
<?php
echo $argv[1];
?>
remember that the first member of the $argv array (which is $argv[0]) is the name of the script itself, so in order to use the parameters for the application, you should start using members of the $argv[] from the '1'th index. when calling the application, use this syntax:
请记住,$ argv数组的第一个成员($ argv [0])是脚本本身的名称,因此为了使用应用程序的参数,您应该开始使用$ argv []的成员。 '1'指数。在调用应用程序时,请使用以下语法:
php myscript.php -- myValue
there is no need to put a name for the parameter. as you saw, what you called the var_dump() on the $argv[], the second member (which is the first parameter) was the string PARAM=TEST. right? so there is no need to put a name for the param. just enter the param value.
没有必要为参数添加名称。如你所见,你在$ argv []上调用var_dump(),第二个成员(第一个参数)是字符串PARAM = TEST。对?所以没有必要为param命名。只需输入参数值。