這篇文章給大家詳細分析了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程序的標準輸出流和結果輸出流均指向終端,所有訊息都列印出來。在瀏覽器端,PHP進程的輸出流被忽略,只有結果資料流被傳送到web伺服器。同時,print和echo呼叫的資訊都會作為執行結果發送到結果輸出流,所以都正常顯示。 最後再感慨一下PHP內建函數的簡潔實用,一個file_put_contents函數就搞定流寫入操作,換Java需要stream/writer一堆程式碼,也省去C風格的fopen/fwrite/fclose的繁瑣。
####
以上是分析php://output和php://stdout的差別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。