php://output與php://stdout有什么不同?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),龍馬潭企業(yè)網(wǎng)站建設(shè),龍馬潭品牌網(wǎng)站建設(shè),網(wǎng)站定制,龍馬潭網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,龍馬潭網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。PHP包含了以php://開頭的一系列輸出輸出流,如php://stdin, php://stdout等。今天查看代碼時(shí),忽然想到一個(gè)問題:php://output和php://stdout有什么區(qū)別?
從PHP的官方文獻(xiàn)中找答案,對(duì)輸入流php://stdin和php://input的解釋分別如下(輸出流的解釋過于簡(jiǎn)略):
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ì)對(duì)比可得出以下信息:1. 均是只讀流; 2. php://stdin是PHP進(jìn)程的標(biāo)準(zhǔn)輸入,php://input用來(lái)讀取請(qǐng)求正文的原始數(shù)據(jù)。通過這些信息,該如何正確認(rèn)識(shí)兩者的本質(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
瀏覽器端請(qǐng)求,輸出如下:
message sent by output message sent by print SAPI:fpm-fcgi
在命令行下,PHP進(jìn)程的標(biāo)準(zhǔn)輸出流和結(jié)果輸出流均指向終端,所有消息都打印出來(lái)。在瀏覽器端,PHP進(jìn)程的輸出流被忽略,只有結(jié)果數(shù)據(jù)流被發(fā)送到web服務(wù)器。同時(shí),print和echo調(diào)用的信息都作為執(zhí)行結(jié)果發(fā)往結(jié)果輸出流,所以都正常顯示。
最后再感慨一下PHP內(nèi)置函數(shù)的簡(jiǎn)潔實(shí)用,一個(gè)file_put_contents函數(shù)就搞定流寫入操作,換Java需要stream/writer一堆代碼,也省去C風(fēng)格的fopen/fwrite/fclose的繁瑣。
看完上述內(nèi)容,你們掌握php://output與php://stdout有什么不同的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享題目:php://output與php://stdout有什么不同-創(chuàng)新互聯(lián)
路徑分享:http://www.rwnh.cn/article28/cegpjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、搜索引擎優(yōu)化、定制網(wǎng)站、網(wǎng)站營(yíng)銷、App開發(fā)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容