分析php://output和php://stdout的区别

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是返回的结果数据流。

下面用代码验证结论:

rush:PHP;"> // file: test.PHP file_put_contents("PHP://output","message sent by output" . PHP_EOL); file_put_contents("PHP://stdout","message sent by stdout" . PHP_EOL); print("message sent by print" . PHP_EOL);

echo "SAPI:",PHP_SAPI,PHP_EOL;

命令行执行文件输出如下:

rush:PHP;"> message sent by output message sent by stdout message sent by print SAPI:cli

浏览器端请求,输出如下:

rush:PHP;"> message sent by output message sent by print SAPI:fpm-fcgi

在命令行下,PHP进程的标准输出流和结果输出流均指向终端,所有消息都打印出来。在浏览器端,PHP进程的输出流被忽略,只有结果数据流被发送到web服务器。同时,print和echo调用的信息都作为执行结果发往结果输出流,所以都正常显示

最后再感慨一下PHP内置函数的简洁实用,一个file_put_contents函数就搞定流写入操作,换Java需要stream/writer一堆代码,也省去C风格的fopen/fwrite/fclose的繁琐。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用