標籤:
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()