PHP之輸出控制 ob_start(),ob_get_contents(),ob_end_clean()

來源:互聯網
上載者:User

標籤:

1.常用函數

  ob_start();#開啟輸出緩衝區

  ob_get_contents();#擷取緩衝區內容

  ob_get_length();#擷取緩衝區內容長度

  ob_clean();#清除之前的所有輸出內容

  ob_end_clean();#清除緩衝區內容並且關閉緩衝區,和ob_clean()差不多吧,不太瞭解真正的區別,但是有人建議使用這個函數

  ob_flush();#沖刷出緩衝區內容,必須在此函數之前使用ob_get_contents()

  ob_get_clean();#實質上是ob_get_contents()和ob_end_clean()一起使用

 

2.通常ob_start(),ob_get_contents(),ob_end_clean()一起使用

<?phpecho ‘This is A‘;ob_end_clean();ob_start();echo ‘This is B‘;$str = ob_get_contents();$len = ob_get_length();ob_end_clean();echo $str.PHP_EOL;echo ‘The length of buffer is ‘.$len.PHP_EOL;

 

 

3.使用ob_end_clean()時最好判斷有沒有緩衝內容,否則會下面的錯誤

1 <?php2 error_reporting(E_ALL);3 4 ob_end_clean();5 6 ob_end_clean();

 

Notice: ob_end_clean(): failed to delete buffer. No buffer to delete in /Users/zongshuai/bhu/ceshi/ceshi.php on line 6

解決辦法 在ob_end_clean()前面先判斷一下,例如 if(ob_get_length()) ob_end_clean(); 或者 if(ob_get_contents()) ob_end_clean();

 

PHP之輸出控制 ob_start(),ob_get_contents(),ob_end_clean()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.