PHP getopt操作 [英] PHP getopt Operations
问题描述
这个问题是关于php中的getopt函数.我需要将两个参数传递给php脚本,例如
This question is regarding getopt function in php. I need to pass two parameter to the php scripts like
php script.php -f filename -t filetype
现在取决于文件类型(可以是u,c或s),我需要进行适当的操作.
Now depending upon the file type which can be u, c or s I need to do proper operation.
我将开关盒用于同一用途:
I am using switch case for the same:
这是我正在使用的代码:
Here is the Code I am using:
// Get filename of input file when executing through command line.
$file = getopt("f:t:");
切换大小写应该比较我从命令行(u,c或i)传入的文件的类型,并相应地匹配它并执行操作.
Switch case should compare the type of the file which I am passing in from the command line (u, c or i) and accordingly match it and do the operation.
switch("I am not sure what should go in there and this is wrong,Please advice")
{
case `Not sure`:
$p->ini();
break;
case `Not sure`:
$p->iniCon();
break;
case `Not sure`:
$p->iniImp();
break;
}
请对此提出建议!
推荐答案
如果只是在PHP脚本(在我的机器上称为temp.php
) 中放入这样的内容:
If you just put something like this in your PHP script (called, on my machine, temp.php
) :
<?php
$file = getopt("f:t:");
var_dump($file);
并从命令行调用它,并传递这两个参数:
And call it from the command-line, passing those two parameters :
php temp.php -f filename -t filetype
您将获得这种输出:
array(2) {
["f"]=>
string(8) "filename"
["t"]=>
string(8) "filetype"
}
这意味着:
-
$file['f']
包含为-f
传递的值
- 和
$file['t']
包含为-t
传递的值
$file['f']
contains the value passed for-f
- and
$file['t']
contains the value passed for-t
从那里开始,如果您希望switch
依赖于作为-t
值传递的选项,则将使用以下内容:
Starting from there, if you want your switch
to depend on the option passed as the value of -t
, you'll use something like this :
switch ($file['t']) {
case 'u':
// ...
break;
case 'c':
// ...
break;
case 'i':
// ...
break;
}
基本上,您放置:
- 您要在
switch()
中测试的变量
- 以及
case
s 中的可能值
- The variable that you want to test in the
switch()
- And the possible values in the
case
s
而且,有关更多信息,您可以阅读PHP手册:
And, for more informations, you can read, in the PHP manual :
这篇关于PHP getopt操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!