php中ob_get_contents();ob_end_clean();ob_start();用法詳解

來源:互聯網
上載者:User
本文介紹下,php中有關ob_get_contents()、ob_end_clean()、ob_start()函數的具體用法,有需要的朋友參考下吧。

php緩衝相關函數:ob_get_contents();ob_end_clean();ob_start()

使用ob_start()把輸出那同輸出到緩衝區,而不是到瀏覽器。然後用ob_get_contents得到緩衝區的資料。

ob_start()在伺服器開啟一個緩衝區來儲存所有的輸出。因此,在任何時候使用echo ,輸出都將被加入緩衝區中,直到程式運行結束或者使用ob_flush()來結束。然後在伺服器中緩衝區的內容才會發送到瀏覽器,由瀏覽器來解析顯示。

函數ob_end_clean 會清除緩衝區的內容,並將緩衝區關閉,但不會輸出內容。此時得用一個函數ob_get_contents()在ob_end_clean()前面來獲得緩衝區的內容。這樣的話,能將在執行ob_end_clean()前把內容儲存到一個變數中,然後在ob_end_clean()後面對這個變數做操作。

例子:

';echo $buf2;?>

下面來看,ob_get_contents

(PHP 4, PHP 5)ob_get_contents -- Return the contents of the output bufferDescriptionstring ob_get_contents ( void )

This will return the contents of the output buffer or FALSE, if output buffering isn't active.

See also ob_start() and ob_get_length().

if you use ob_start with a callback function as a parameter, and that function changes ob string (as in example in manual) don't expect that ob_get_contents will return changed ob.it will work as you would use ob_start with no parameter at all. So don't be confused.transfer image, another method (alternative to fsockopen or function socket) :

server(192.168.0.1)makeimage.php

naturally you can transfer whichever thing and not only images

ob_get_clean

(PHP 4 >= 4.3.0, PHP 5)ob_get_clean -- Get current buffer contents and delete current output bufferDescriptionstring ob_get_clean ( void )

This will return the contents of the output buffer and end output buffering. If output buffering isn't active then FALSE is returned. ob_get_clean() essentially executes both ob_get_contents() and ob_end_clean().

例1:ob_get_clean()的簡單樣本

輸出: string(11) "hello world"

例2,ob_start() and ob_get_contents().Notice that the function beneath does not catch errors, so throw in an @ before those ob_* callsRunning PHP4 < 4.3.0, you can simply add the following to use the function anyway:

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.