#!/usr/bin/php -q
<?php
set_time_limit(0);
@ob_end_flush();
ob_implicit_flush(true);
class prompt {
var $tty;
function prompt() {
if (substr(PHP_OS, 0, 3) == "WIN") {
$this->tty = fOpen("\con", "rb");
} else {
if (!($this->tty = fOpen("/dev/tty", "r"))) {
$this->tty = fOpen("php://stdin", "r");
}
}
}
function get($string, $length = 1024) {
echo $string;
$result = trim(fGets($this->tty, $length));
echo "\n";
return $result;
}
}
echo "Enter command or 'exit' to quit\n";
do {
$cmdline = new prompt();
$buffer = $cmdline->get("PHP: ");
echo "accept cmd : $buffer\n";
} while ($buffer !== "exit");
echo "waiting for exit....\n";
?>
本文介绍了一个使用PHP编写的命令行交互程序,该程序通过自定义的prompt类实现与用户的交互,包括输入命令并根据输入内容进行响应。适用于学习PHP命令行应用开发。
3621

被折叠的 条评论
为什么被折叠?



