<记录> PHP读取命令行参数

方法一:使用$argv or $argc参数接收

echo "接收到{$argc}个参数";
print_r($argv);
[root@DELL113 lee]# /usr/local/php/bin/php test.php
接收到1个参数Array
(
    [0] => test.php
)
[root@DELL113 lee]# /usr/local/php/bin/php test.php a b c d
接收到5个参数Array
(
    [0] => test.php
    [1] => a
    [2] => b
    [3] => c
    [4] => d
)

  

方法二:使用getopt函数

$param_arr = getopt('a:b:');
print_r($param_arr);
[root@DELL113 lee]# /usr/local/php/bin/php test.php -a 345
Array
(
    [a] => 345
)
[root@DELL113 lee]# /usr/local/php/bin/php test.php -a 345 -b 12q3
Array
(
    [a] => 345
    [b] => 12q3
)
[root@DELL113 lee]# /usr/local/php/bin/php test.php -a 345 -b 12q3 -e 3322ff
Array
(
    [a] => 345
    [b] => 12q3
)

  

方法三:提示用户输入

fwrite(STDOUT,'请输入您的博客名:');
echo '您输入的信息是:'.fgets(STDIN);
原文地址:https://www.cnblogs.com/xiaoliwang/p/9491271.html