国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

php---output和php---stdout使用區別詳解

2020-03-22 18:59:32
字體:
來源:轉載
供稿:網友
這次給大家帶來php://output和php://stdout使用區別詳解,php://output和php://stdout使用的注意事項有哪些,下面就是實戰案例,一起來看一下。

從PHP的官方文獻中找答案,對輸入流php://stdin和php://input的解釋分別如下(輸出流的解釋過于簡略):

php://stdinphp://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://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_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.phpfile_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 outputmessage sent by stdoutmessage sent by printSAPI:cli

瀏覽器端請求,輸出如下:

message sent by outputmessage sent by printSAPI:fpm-fcgi

在命令行下,PHP進程的標準輸出流和結果輸出流均指向終端,所有消息都打印出來。在瀏覽器端,PHP進程的輸出流被忽略,只有結果數據流被發送到web服務器。同時,print和echo調用的信息都作為執行結果發往結果輸出流,所以都正常顯示。

相信看了本文案例你已經掌握了方法,更多精彩請關注 其它相關文章!

推薦閱讀:

PHP計算特大數字的整數集

PHP+ajax實現獲取新聞數據案例詳解

以上就是php://output和php://stdout使用區別詳解的詳細內容,更多請關注 其它相關文章!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 敦煌市| 张家界市| 鄯善县| 凉山| 资兴市| 故城县| 太白县| 贺州市| 阿巴嘎旗| 江源县| 辽宁省| 定南县| 高安市| 巴楚县| 息烽县| 理塘县| 蕲春县| 长寿区| 安阳市| 如皋市| 长宁区| 甘孜县| 麻城市| 宜阳县| 教育| 进贤县| 巴青县| 阜新市| 华亭县| 渭南市| 海林市| 定日县| 达尔| 松阳县| 峡江县| 丰宁| 镇雄县| 潞城市| 灵宝市| 合山市| 稻城县|