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


php可以在cli(命令行)模式里运行,不需要人和一种web服务器做载体

在cli模式里如何获取传入的参数呢?

1、使用全局参数$argv接受参数(register_argc_argv打开时可用)

cli : /php/xxx.php one two three

print_r($argv)

打印:array(
            [0] => /php/xxx.php
            [1] => one 
            [2] => two 
            [3] => three 
        )

第一个参数为文件路径,其余为传入参数($argc为参数个数,所以最小为1)

2、使用getopt()函数获取参数

cli : /php/xxx.php -a one -b two -c three

//获取 -a -b -c参数
$args = getopt('a:b:c:');
print_r($args);

打印:array(
            [a] => one 
            [b] => two 
            [c] => three 
        )