这个问题是关于 php 中的 getopt 函数。我需要将两个参数传递给 php 脚本,例如
php script.php -f filename -t filetype
现在,根据文件类型(可以是 u、c 或 s),我需要执行正确的操作。
我正在使用 switch case 来实现相同的目的:
这是我正在使用的代码:
// Get filename of input file when executing through command line.
$file = getopt("f:t:");
Switch case 应该比较我从命令行传入的文件类型(u、c 或 i),并相应地匹配它并执行操作。
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
) :
<?php
$file = getopt("f:t:");
var_dump($file);
并从命令行调用它,传递这两个参数:
php temp.php -f filename -t filetype
你会得到这样的输出:
array(2) {
["f"]=>
string(8) "filename"
["t"]=>
string(8) "filetype"
}
意思就是 :
-
$file['f']
包含传递给的值-f
- and
$file['t']
包含传递给的值-t
从那里开始,如果你想要你的switch
取决于作为值传递的选项-t
,你将使用这样的东西:
switch ($file['t']) {
case 'u':
// ...
break;
case 'c':
// ...
break;
case 'i':
// ...
break;
}
基本上,你把:
- 您要在其中测试的变量
switch()
- 以及可能的值
case
s
并且,有关更多信息,您可以阅读 PHP 手册:
- The 的手册页switch
- And getopt()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)