/*
* 方法一 使用$argc $argv
* 在命令行下运行 /usr/local/php/bin/php ./getopt.php -f 123 -g 456
*/
// if ($argc > 1){
// print_r($argv);
// }
/**
* 运行结果
*
sync@MySUSE11:~/web_app/channel3/interface> /usr/local/php/bin/php ./getopt.php -f 123 -g 456
Array
(
[0] => ./getopt.php
[1] => -f
[2] => 123
[3] => -g
[4] => 456
)
*/
/*
* 方法二 使用getopt函数()
* 在命令行下运行 /usr/local/php/bin/php ./getopt.php -f 123 -g 456
*/
// $options = "f:g:";
// $opts = getopt( $options );
// print_r($opts);
/**
* 运行结果
*
sync@MySUSE11:~/web_app/channel3/interface> /usr/local/php/bin/php ./getopt.php -f 123 -g 456
Array
(
[f] => 123
[g] => 456
)
*/
/*
* 方法三 提示用户输入,然后获取输入的参数。有点像C语言
* 在命令行下运行 /usr/local/php/bin/php ./getopt.php
*/
fwrite(STDOUT, "Enter your name: ");
$name = trim(fgets(STDIN));
fwrite(STDOUT, "Hello, $name!");
/**
* 运行结果
*
sync@MySUSE11:~/web_app/channel3/interface> /usr/local/php/bin/php ./getopt.php
Enter your name: francis
Hello, francis!
*/
?>
最后修改于 2012-03-28 13:40
阅读(?)评论(0)