【Thinkphp】 CLI模式下接收参数的几种方式

PHP 中 CLI模式下接收参数的三大方式

1 argv 方式

php test.php news 1 5
//变量仅在 register_argc_argv 打开时可用。
print_r($argc); //CLI下获取参数的数目,最小值为1
print_r($argv); //CLI下传递给脚本的参数数组,第一个参数总是当前脚本的文件名,因此 $argv[0] 就是脚本文件名
//结果
Array
(
    [0] => test.php
    [1] => news
    [2] => 1
    [3] => 5
)

2 getopt 方式

php test.php -a 1 -b 2 -c 3 -d=4 -e 5
$param = getopt('a:b:c:d::e');
print_r($param);

//结果

Array
(
    [a] => 1
    [b] => 2
    [c] => 3
    [d] => 4
    [e] => 
)

3 longopts 方式

php test.php --type news --is_hot 1 --limit=10 --expire=100
$longopt = array(
    'type:',
    'is_hot:',
    'limit::',
    'expire'
);
$param = getopt('', $longopt);
print_r($param);
//结果
Array
(
    [type] => news
    [is_hot] => 1
    [limit] => 10
    [expire] => 
)

注意这种

找到第一非选项,后面忽略实例

php test.php --type news --is_hots 1 --limit=10 --expire=100
$longopt = array(
    'type:',
    'is_hot:',
    'limit::',
    'expire'
);
$param = getopt('', $longopt);
print_r($param);
//结果
Array
(
    [type] => news
)
//因为is_hots不是选项值(定义的是is_hot),所以从这里开始之后的参数,都被丢弃

在thinkphp 中, 传参 可以用 这种

php  www/public/index.php  index/Test/run/page/111/page2/222/page3/333
等效于
www.test.com/index/Test/run?page=111&page2=222&page3=333

此处的 page 就相当于 get 方式请求的参数

posted @ 2021-01-05 15:56  依然范儿特西  阅读(741)  评论(0编辑  收藏  举报