加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP getopt操作

发布时间:2020-12-13 18:14:48 所属栏目:PHP教程 来源:网络整理
导读:这个问题是关于php中的getopt函数.我需要将两个参数传递给php脚本,如 php script.php -f filename -t filetype 现在根据文件类型可以是u,c或s我需要做正确的操作. 我正在使用相同的开关盒: 这是我正在使用的代码: // Get filename of input file when exec
这个问题是关于php中的getopt函数.我需要将两个参数传递给php脚本,如
php script.php -f filename -t filetype

现在根据文件类型可以是u,c或s我需要做正确的操作.

我正在使用相同的开关盒:

这是我正在使用的代码:

// Get filename of input file when executing through command line.
$file = getopt("f:t:");

切换案例应该比较我从命令行(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传递的值
>和$file [‘t’]包含为-t传递的值

从那里开始,如果你想让你的开关依赖于作为-t值传递的选项,你将使用这样的东西:

switch ($file['t']) {
    case 'u':
            // ...
        break;
    case 'c':
            // ...
        break;
    case 'i':
            // ...
        break;
}

基本上,你把:

>要在switch()中测试的变量
>以及案例中的可能值

而且,有关更多信息,您可以在PHP手册中阅读:

> manual page of switch
>和getopt()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读