這篇文章給大家詳細分析了php://output和php://stdout的用法差異以及實例程式碼分享,有需要的朋友可以參考學習下。
PHP包含了以php://開頭的一系列輸出輸出流,如php://stdin, php://stdout等。今天查看程式碼時,突然想到一個問題:php://output和php://stdout有什麼差別?
從PHP的官方文獻中找答案,輸入流php://stdin和php://input的解釋分別如下(輸出流的解釋過於簡略):
文件並未直接闡述兩者的區別,仔細對比可得出以下資訊: 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是傳回的結果資料流。 下面用程式碼驗證結論:#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 thisgy behavior 5. 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://stdin is read-only, whereas php://stdout and php://stderr are write-only.php://inputphp://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_RAF_POST_DATA is not , those isdefension $HTTP_unper 月, DDATA is notive, DDATA is), defulion orem. to activating always_populate_raw_post_data. php://input is not available with enctype=”multipart/form-data”.
// 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://output和php://stdout的差別的詳細內容。更多資訊請關注PHP中文網其他相關文章!