php output详解,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是返回的结果数据流。

下面用代码验证结论:

// 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;

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

message sent by output

message sent by stdout

message sent by print

SAPI:cli

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

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的繁琐。

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`snd_output_stdio_attach` 函数是 ALSA 库中的一个函数,用于将音频数据输出到标准输出流(stdout)或标准错误流(stderr)。 该函数的原型如下: ```c int snd_output_stdio_attach(snd_output_t **output, FILE *fp, int close_fp); ``` 其中: - `output`:输出参数,指向新创建的 snd_output_t 对象的指针。 - `fp`:输出流指针,可以是 stdout 或 stderr。 - `close_fp`:是否在关闭输出对象时关闭流,非 0 表示关闭。 该函数会创建一个新的 `snd_output_t` 对象,并将其与指定的输出流关联起来。在之后的输出操作中,音频数据将被写入到该流中。 使用该函数时,需要注意以下几点: - 调用该函数前,需要先初始化 ALSA 库,通常是通过调用 `snd_config_update_free_global` 函数来完成的。 - 在输出完成后,需要调用 `snd_output_close` 函数来关闭输出对象,释放资源。 - 该函数返回 0 表示成功,否则表示失败。 下面是一个示例代码: ```c #include <alsa/asoundlib.h> #include <stdio.h> int main() { snd_output_t *output = NULL; int ret = snd_output_stdio_attach(&output, stdout, 1); if (ret != 0) { printf("Failed to attach output to stdout\n"); return -1; } snd_output_printf(output, "Hello, world!\n"); snd_output_close(output); return 0; } ``` 该代码会将字符串 "Hello, world!\n" 输出到标准输出流中。注意,在输出完成后,需要调用 `snd_output_close` 函数来关闭输出对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值