PHP standard input?(PHP标准输入?)
问题描述
我知道 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标准输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP标准输入?


- PHP foreach() 与数组中的数组? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01