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

一、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
)

 

二、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] => 
)

 

三、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
    A+
发布日期:2021年01月11日 11:40:10  所属分类:ThinkPHP框架
最后更新时间:2021-01-11 11:40:27
头像
  • ¥ 69.0元
  • 市场价:69.0元
  • ¥ 199.0元
  • 市场价:399.0元
  • ¥ 79.0元
  • 市场价:99.0元
  • ¥ 159.0元
  • 市场价:599.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: