加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

说明php://output和php://stdout的区别

发布时间:2021-05-19 03:29:06 所属栏目:编程 来源:网络整理
导读:PHP包括了以php://开头的一系列输出输出流,如php://stdin,php://stdout等。本日查察代码时,突然想到一个题目:php://output和php://stdout有什么区别? 从PHP的官方文献中找谜底,对输入流php://stdin和php://input的表明别离如下(输出流的表明过于大略)

PHP包括了以php://开头的一系列输出输出流,如php://stdin,php://stdout等。本日查察代码时,突然想到一个题目:php://output和php://stdout有什么区别?

从PHP的官方文献中找谜底,对输入流php://stdin和php://input的表明别离如下(输出流的表明过于大略):

php://stdin

php://stdin,php://stdout and php://stderr allow direct access to the corresponding input or output stream of the PHP process. The stream references a duplicate file descriptor,so if you open php://stdin and later close it,you close only your copy of the descriptor-the actual stream referenced by STDIN is unaffected. Note that PHP exhibited buggy behavior in this regard until PHP 5.2.1. It is recommended that you simply use the constants STDIN,STDOUT and STDERR instead of manually opening streams using these wrappers.

php://stdin is read-only,whereas php://stdout and php://stderr are write-only.

php://input

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests,it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover,for those cases where $HTTP_RAW_POST_DATA is not populated by default,it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype=”multipart/form-data”.

文档并未直接叙述两者的区别,细心比拟可得出以下信息:1. 均是只读流; 2. php://stdin是PHP历程的尺度输入,php://input用来读取哀求正文的原始数据。通过这些信息,该怎样正确熟悉两者的本质区别?

顺着php://stdin历程输入的提醒,遐想PHP历程的执行进程,再团结SAPI的差别,可以获得两者首要区别:php://stdin是PHP历程的输入流,执行生命周期内均也许稀有据流入(譬喻CLI下的交互式输入);php://input是PHP执行时的外部输入流,一样平常数据只能读一次(详细看SAPI的实现)。同理可获得php://stdout和php://output的区别:php://stdout是PHP历程的尺度输出流,php://output是返回的功效数据流。

下面用代码验证结论:

echo "SAPI:",PHP_SAPI,PHP_EOL;

呼吁行执行文件,输出如下:

赏识器端哀求,输出如下:

在呼吁行下,PHP历程的尺度输出流和功效输出流均指向终端,全部动静都打印出来。在赏识器端,PHP历程的输出流被忽略,只有功效数据流被发送到web处事器。同时,print和echo挪用的信息都作为执行功效发往功效输出流,以是都正常表现。

最后再感应一下PHP内置函数的简捷适用,一个file_put_contents函数就搞定流写入操纵,换Java必要stream/writer一堆代码,也省去C气魄威风凛凛的fopen/fwrite/fclose的繁琐。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读