亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

php包含了以php://開頭的一系列輸出輸出流,如php://stdin, php://stdout等。今天查看代碼時(shí),忽然想到一個(gè)問題:php://output和php://stdout有什么區(qū)別?從PHP的官方文獻(xiàn)中找答案,對輸入流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".

文檔并未直接闡述兩者的區(qū)別,仔細(xì)對比可得出以下信息:

1. 均是只讀流;

2. php://stdin是PHP進(jìn)程的標(biāo)準(zhǔn)輸入,php://input用來讀取請求正文的原始數(shù)據(jù)。

通過這些信息,該如何正確認(rèn)識兩者的本質(zhì)區(qū)別?

順著php://stdin進(jìn)程輸入的提示,聯(lián)想PHP進(jìn)程的執(zhí)行過程,再結(jié)合SAPI的差異,可以得到兩者主要區(qū)別:

php://stdin是PHP進(jìn)程的輸入流,執(zhí)行生命周期內(nèi)均可能有數(shù)據(jù)流入(例如CLI下的交互式輸入);

php://input是PHP執(zhí)行時(shí)的外部輸入流,一般數(shù)據(jù)只能讀一次(具體看SAPI的實(shí)現(xiàn))。

同理可得到php://stdout和php://output的區(qū)別:php://stdout是PHP進(jìn)程的標(biāo)準(zhǔn)輸出流,php://output是返回的結(jié)果數(shù)據(jù)流。

下面用代碼驗(yàn)證結(jié)論:

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

命令行執(zhí)行文件,輸出如下:

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進(jìn)程的標(biāo)準(zhǔn)輸出流和結(jié)果輸出流均指向終端,所有消息都打印出來。在瀏覽器端,PHP進(jìn)程的輸出流被忽略,只有結(jié)果數(shù)據(jù)流被發(fā)送到web服務(wù)器。同時(shí),print和echo調(diào)用的信息都作為執(zhí)行結(jié)果發(fā)往結(jié)果輸出流,所以都正常顯示。

最后再感慨一下PHP內(nèi)置函數(shù)的簡潔實(shí)用,一個(gè)file_put_contents函數(shù)就搞定流寫入操作,換JAVA需要stream/writer一堆代碼,也省去C風(fēng)格的fopen/fwrite/fclose的繁瑣。

分享到:
標(biāo)簽:php
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定