php cli模式下获取参数的方法

                       

php在cli模式下接收参数有两种方法

1.使用argv数组
2.使用getopt方法

1.使用argv数组

例如:需要执行一个php,并传递三个参数(type=news, is_hot=1, limit=5)

创建test.php

<?phpprint_r($argv);?>
  
  
  • 1
  • 2
  • 3

在命令行执行

php test.php news 1 5
  
  
  • 1

输出:

Array(    [0] => test.php    [1] => news    [2] => 1    [3] => 5)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

可以看到argv[0]为当前执行的php文件名称,而argv[1]~argv[3]则是传递的参数的值
argv[1]等于type的值
argv[2]等于is_hot的值
argv[3]等于limit的值
这样可以根据argv数组来获取传递的参数进行后续的处理操作。

缺点:
使用argv数组,可以按顺序获取传递的参数。但获取后,需要做一个对应处理,上例中需要把argv[1]对应type参数,argv[2]对应is_hot参数,argv[3]对应limit参数。而如果在传递的过程中,参数顺序写错,则会导致程序出错。

例如:

<?php$param = array();$param['type'] = $argv[1];$param['is_hot'] = $argv[2];$param['limit'] = $argv[3];print_r($param);?>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

执行

php test.php news 1 5
  
  
  • 1

输出:

Array(    [type] => news    [is_hot] => 1    [limit] => 5)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

而传递顺序不同,获取到的参数数值会不同,导致后续程序出错

执行

php test.php 1 5 news
  
  
  • 1

输出:

Array(    [type] => 1    [is_hot] => 5    [limit] => news)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

因此在使用argv数组传递参数时,需要注意参数传递的顺序。

2.使用getopt方法

getopt 从命令行参数列表中获取选项

array getopt ( string $options [, array $longopts ] )
  
  
  • 1

参数:
options
该字符串中的每个字符会被当做选项字符,匹配传入脚本的选项以单个连字符(-)开头。 比如,一个选项字符串 “x” 识别了一个选项 -x。 只允许 a-z、A-Z 和 0-9。

longopts
选项数组。此数组中的每个元素会被作为选项字符串,匹配了以两个连字符(–)传入到脚本的选项。 例如,长选项元素 “opt” 识别了一个选项 –opt。

options 可能包含了以下元素:
单独的字符(不接受值)
后面跟随冒号的字符(此选项需要值)
后面跟随两个冒号的字符(此选项的值可选)
选项的值是字符串后的第一个参数。它不介意值之前是否有空格。

options 和 longopts 的格式几乎是一样的,唯一的不同之处是 longopts 需要是选项的数组(每个元素为一个选项),而 options 需要一个字符串(每个字符是个选项)。
传值的分隔符可以使用空格或=。
可选项的值不接受空格作为分隔符,只能使用=作为分隔符。

返回值
此函数会返回选项/参数对,失败时返回 FALSE。
选项的解析会终止于找到的第一个非选项,之后的任何东西都会被丢弃。

1.使用options实例

a,b,c 为需要值
d 为可选值
e 为不接受值

<?php$param = getopt('a:b:c:d::e');print_r($param);?>
  
  
  • 1
  • 2
  • 3
  • 4

执行

php test.php -a 1 -b 2 -c 3 -d=4 -e 5
  
  
  • 1

输出:

Array(    [a] => 1    [b] => 2    [c] => 3    [d] => 4    [e] => )
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8


2.使用longopts实例

type,is_hot 为需要值
limit 为可选值
expire 为不接受值

<?php$longopt = array(    'type:',    'is_hot:',    'limit::',    'expire');$param = getopt('', $longopt);print_r($param);?>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

执行

php test.php --type news --is_hot 1 --limit=10 --expire=100
  
  
  • 1

输出:

Array(    [type] => news    [is_hot] => 1    [limit] => 10    [expire] => )
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7


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

<?php$longopt = array(    'type:',    'is_hot:',    'limit::',    'expire');$param = getopt('', $longopt);print_r($param);?>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

执行

php test.php --type news --is_hots 1 --limit=10 --expire=100
  
  
  • 1

输出:

Array(    [type] => news)
  
  
  • 1
  • 2
  • 3
  • 4

因为is_hots不是选项值(定义的是is_hot),所以从这里开始之后的参数,都被丢弃。


总结:
使用argv数组传参数,方法简单,实现方便。参数的顺序不能错,参数获取后需要做对应处理。
使用getopt方法,可使用参数名,参数顺序可随意,比较规范。(建议使用)

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值