Blogger Information
Blog 143
fans 1
comment 0
visits 440083
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP命令行脚本接收传入参数的三种方式
弘德誉曦的博客
Original
1048 people have browsed it

通常PHP都做http方式请求了,可以使用GET or POST方式接收参数,有些时候需要在shell命令下把PHP当作脚本执行,比如定时任务。这就涉及到在shell命令下如何给php传参的问题,通常有三种方式传参。
一、使用$argv or $argc参数接收

/**
 * 使用 $argc $argv 接受参数 */
 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函数

/**
 * 使用 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
)
 
三、提示用户输入
复制代码
<?.fgets(STDIN);
复制代码

执行

[root@DELL113 lee]# /usr/local/php/bin/php test.php
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post