php緩沖輸出實例分析
更新時間:2015年01月05日 14:16:39 投稿:shichen2014
這篇文章主要介紹了php緩沖輸出用法,以實例形式較為完整的分析了緩沖輸出的具體實現(xiàn)過程,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php緩沖輸出用法。分享給大家供大家參考。具體分析如下:
ob_start([string output_callback])- 打開輸出緩沖區(qū)
所有的輸出信息不在直接發(fā)送到瀏覽器,而是保存在輸出緩沖區(qū)里面,可選得回調(diào)函數(shù)用于處理輸出結(jié)果信息.
ob_end_flush - 結(jié)束(發(fā)送)輸出緩沖區(qū)的內(nèi)容,關(guān)閉輸出緩沖區(qū)
實例代碼如下:
復(fù)制代碼 代碼如下:
ob_start(); //打開緩沖區(qū)
echo "hello world"; //輸出內(nèi)容
$out=ob_get_clean(); //獲得緩沖區(qū)內(nèi)容并且結(jié)束緩沖區(qū)
$out=strtolower($out); //將字符轉(zhuǎn)換為小寫
var_dump($out); //輸出結(jié)果
//
if(!function_exists('ob_clean')) //判斷函數(shù)是否被定義
{
function ob_clean() //定義函數(shù)
{
if(@ob_end_clean())
{
return ob_start();
}
trigger_error("ob_clean() failed to delete buffer.no buffer to delete.",e_user_notice);
return false;
}
}
//
header('content-type: multipart/x-mixed-replace;boundary=endofsection'); //發(fā)送標頭
print "n--endofsectionn"; //輸出內(nèi)容
$pmt=array("-","","|","/"); //定義數(shù)組
for($i=0;$i<10;$i++) //通過循環(huán)進行操作
{
sleep(1); //暫停執(zhí)行
print "content-type: text/plainnn"; //輸出內(nèi)容
print "part $it".$pmt[$i % 4]; //輸出內(nèi)容
print "--endofsectionn"; //輸出內(nèi)容
ob_flush(); //發(fā)送緩沖區(qū)數(shù)據(jù)
flush(); //刷新輸出緩沖
}
print "content-type: text/plainnn"; //輸出內(nèi)容
print "the endn"; //輸出內(nèi)容
print "--endofsection--n"; //輸出內(nèi)容
echo "hello world"; //輸出內(nèi)容
$out=ob_get_clean(); //獲得緩沖區(qū)內(nèi)容并且結(jié)束緩沖區(qū)
$out=strtolower($out); //將字符轉(zhuǎn)換為小寫
var_dump($out); //輸出結(jié)果
//
if(!function_exists('ob_clean')) //判斷函數(shù)是否被定義
{
function ob_clean() //定義函數(shù)
{
if(@ob_end_clean())
{
return ob_start();
}
trigger_error("ob_clean() failed to delete buffer.no buffer to delete.",e_user_notice);
return false;
}
}
//
header('content-type: multipart/x-mixed-replace;boundary=endofsection'); //發(fā)送標頭
print "n--endofsectionn"; //輸出內(nèi)容
$pmt=array("-","","|","/"); //定義數(shù)組
for($i=0;$i<10;$i++) //通過循環(huán)進行操作
{
sleep(1); //暫停執(zhí)行
print "content-type: text/plainnn"; //輸出內(nèi)容
print "part $it".$pmt[$i % 4]; //輸出內(nèi)容
print "--endofsectionn"; //輸出內(nèi)容
ob_flush(); //發(fā)送緩沖區(qū)數(shù)據(jù)
flush(); //刷新輸出緩沖
}
print "content-type: text/plainnn"; //輸出內(nèi)容
print "the endn"; //輸出內(nèi)容
print "--endofsection--n"; //輸出內(nèi)容
希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
不用mod_rewrite直接用php實現(xiàn)偽靜態(tài)化頁面代碼
不用mod_rewrite直接用php代碼實現(xiàn)偽靜態(tài)效果,大家看后就會發(fā)現(xiàn)php真的很方便2008-10-10