php PHP标准输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/554760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:04:14  来源:igfitidea点击:

PHP standard input?

phpfile-iostdin

提问by Chris Lutz

I know PHP is usually used for web development, where there isno 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 printand 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通常用于web开发,那里没有标准的输入,但是PHP声称是可用作通用脚本语言,如果你遵循它的古怪基于web的约定。我知道 PHPstdout使用printand打印到(或您想调用的任何内容)echo,这很简单,但我想知道 PHP 脚本如何从stdin(特别是使用fgetc(),但任何输入函数都很好)获取输入,或者是这甚至可能吗?

回答by Patrick Glandien

It is possible to read the stdinby creating a file handle to php://stdinand then read from it with fgets()for a line for example (or, as you already stated, fgetc()for a single character):

可以stdin通过创建一个文件句柄来php://stdin读取它,然后用fgets()一行读取它(或者,正如你已经说过的,fgetc()对于单个字符):

<?php
$f = fopen( 'php://stdin', 'r' );

while( $line = fgets( $f ) ) {
  echo $line;
}

fclose( $f );
?>

回答by anatoly techtonik

Reading from STDINis recommended way

STDIN读取是推荐的方式

<?php
while (FALSE !== ($line = fgets(STDIN))) {
   echo $line;
}
?>

回答by mjs

To avoid having to mess around with filehandles, use file_get_contents()and php://stdin:

为避免弄乱文件句柄,请使用file_get_contents()php://stdin

$ echo 'Hello, World!' | php -r 'echo file_get_contents("php://stdin");'
Hello, World!

(If you're reading a truly huge amount of data from stdinyou might want to use the filehandle approach, but this should be good for many megabytes.)

(如果您正在从真正大量的数据中读取数据,stdin您可能想要使用文件句柄方法,但这对于多兆字节来说应该是有好处的。)

回答by Muhammad Usman

A simple method is

一个简单的方法是

$var = trim(fgets(STDIN));

回答by redolent

Grab it all in one shot:

一枪搞定:

$contents = file_get_contents("php://stdin");
echo $contents;

回答by Greg

You can use fopen()on php://stdin:

您可以使用fopen()php://stdin

$f = fopen('php://stdin', 'r');

回答by Fritz H

IIRC, you may also use the following:

IIRC,您还可以使用以下内容:

$in = fopen(STDIN, "r");
$out = fopen(STDOUT, "w");

Technically the same, but a little cleaner syntax-wise.

技术上相同,但在语法上更简洁一些。

回答by Richard Cross

This also works:

这也有效:

$data = stream_get_contents(STDIN);

回答by NVRM

When using fgets, it may block in bash scripts, if the stdinisn't set or empty, including while using the @php error control operator.

使用 fgets 时,它可能会阻塞 bash 脚本,如果stdin未设置或为空,包括在使用@php 错误控制运算符时

#!/usr/bin/php
<?php
$pipe = @trim(fgets(STDIN));
// Script was called with an empty stdin
// Fail to continue, php warning 

This behavior can be avoided by setting stream_set_blockingon the php header:

可以通过stream_set_blocking在 php 标头上设置来避免这种行为:

#!/usr/bin/php
<?php
stream_set_blocking(STDIN, 0);
$pipe = @trim(fgets(STDIN));
// Script was called with an empty stdin
// No errors or warnings, continue 
echo $pipe . "!";

As example, to be called as follow:

例如,调用如下:

echo "Hello world" | ./myPHPscript
// Output "Hello world!"
./myPHPscript
// Output "!"

回答by Donovan P

Instead of manually opening STDIN stream, use built in readline() function if you just want to read a single line without too much a hassle :

如果您只想读取一行而不会太麻烦,请使用内置的 readline() 函数,而不是手动打开 STDIN 流:

<?php
$age= readline("Enter your age: ");
echo "Your age is : ".$age;

PHP documentation is your friend : https://www.php.net/manual/en/function.readline.php

PHP 文档是您的朋友:https: //www.php.net/manual/en/function.readline.php