PHP的标准输入? [英] PHP standard input?
问题描述
我知道PHP通常用于web开发,其中 没有标准输入,但是PHP声称可以用作通用脚本语言,如果你确实遵循它的基于web的时髦约定。我知道PHP使用 print
和打印到
,这很简单,但我想知道PHP脚本如何从 stdout
(或者任何你想要调用它的东西) > echo stdin
(特别是使用 fgetc()
,但任何输入函数是好的),或者这是甚至可能的?
I know PHP is usually used for web development, where there is no standard input, but PHP claims to be usable as a general-purpose scripting language, if you do follow it's funky web-based conventions. I know that PHP prints to stdout
(or whatever you want to call it) with print
and echo
, which is simple enough, but I'm wondering how a PHP script might get input from stdin
(specifically with fgetc()
, but any input function is good), or is this even possible?
推荐答案
通过创建文件句柄 php:// stdin
,读取 stdin
然后读取(或者,正如你已经说过的, fgetc()
用于单个字符):
It is possible to read the stdin
by creating a file handle to php://stdin
and then read from it with fgets()
for a line for example (or, as you already stated, fgetc()
for a single character):
<?php
$f = fopen( 'php://stdin', 'r' );
while( $line = fgets( $f ) ) {
echo $line;
}
fclose( $f );
?>
这篇关于PHP的标准输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!