Warning: curl_exec() has been disabled for security reasons in /pub/host/sunboyu/sunboyu/www/wp-includes/http.php on line 1022
PHP CLI应用进阶 » Blog Archive 一个程序猿 孙小一,孙小二,PHP,MYSQL,LINUX,APACHE,原创技术,扯淡

PHP CLI应用进阶

作者 : admin 于 2008-06-13 12:51:50
2008
06-13

上篇介绍了命令行参数的传递,参考php官方手册后,发现php的cli也是可以跟用户进行交互的。

http://cn.php.net/manual/en/features.commandline.php

To ease working in the shell environment, the following constants are defined:

CLI specific Constants
Constant Description
STDIN

An already opened stream to stdin. This saves opening it with

<?php

$stdin

= fopen(‘php://stdin’, ‘r’);

?>

If you want to read single line from stdin, you can use

<?php
$line
= trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN, “%d\n”, $number); // reads number from STDIN
?>
STDOUT

An already opened stream to stdout. This saves opening it with

<?php

$stdout

= fopen(‘php://stdout’, ‘w’);

?>

STDERR

An already opened stream to stderr. This saves opening it with

<?php

$stderr

= fopen(‘php://stderr’, ‘w’);

?>

我比较倾向这种方式,这样就跟c++类似了

  1. <?php
  2. $line = trim(fgets(STDIN)); // reads one line from STDIN
  3. fscanf(STDIN, "%d\n", $number); // reads number from STDIN
  4. ?>

发表评论




XHTML:你可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(若看不到验证码,请重新加载页面。)