問題描述
我知道 PHP 通常用于 Web 開發,其中 沒有標準輸入,但 PHP 聲稱可用作通用腳本語言,如果您遵循它是基于 Web 的時髦公約.我知道 PHP 使用 print
和 echo
打印到 stdout
(或任何你想調用的東西),這很簡單,但我我想知道 PHP 腳本如何從 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
和然后使用 fgets()
從中讀取一行(或者,正如您已經說過的,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標準輸入?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!