是否可以单独使用 PHP 创建交互式 shell?
我的意思是像数据库一样,Python https://en.wikipedia.org/wiki/Python_%28programming_language%29等等。如果是的话,怎么样?
是的,这是可能的。为了实现交互,程序必须能够等待并读取用户输入stdin https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)。在 PHP 中,您可以读取stdin通过打开文件描述符'php://stdin' http://php.net/stdin。取自对不同问题的回答 https://stackoverflow.com/questions/5638677/php-5-second-countdown-cli-not-javascript/5639075#5639075,这是 PHP 中的交互式用户提示的示例(当然是从命令行运行时):
echo "Continue? (Y/N) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
if ($response != 'Y') {
echo "Aborted.\n";
exit;
}
当然,要获得整行输入而不是单个字符,您需要fgets() http://php.net/fgets代替fgetc() http://php.net/fgetc。根据您的程序/shell 将执行的操作,整个程序可能会被构造为一个大的连续循环。希望这能让您了解如何开始。如果你想得到really花哨的(CLI 伪 GUI),你可以使用 ncurses http://devzone.zend.com/article/1083.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)