欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP嵌套輸出緩沖代碼實(shí)例

 更新時(shí)間:2015年05月12日 11:41:03   投稿:junjie  
這篇文章主要介紹了PHP嵌套輸出緩沖代碼實(shí)例,本文講解嵌套使用ob系列函數(shù)的實(shí)例,需要的朋友可以參考下

PHP的輸出緩存是可以嵌套的。用ob_get_level()就可以輸出嵌套級(jí)別。
測(cè)試發(fā)現(xiàn)在cli和瀏覽器下輸出結(jié)果不一樣(PHP5.4)。

手冊(cè)說(shuō)明如下:

ob_get_level() will always return 0 inside a destructor.
This happens because the garbage collection for output buffers has already done before the destructor is called

想要正確輸出也很簡(jiǎn)單:

復(fù)制代碼 代碼如下:

ob_end_clean();
echo ob_get_level(); //0

回到正題:

復(fù)制代碼 代碼如下:

ob_end_clean();
 
ob_start();
echo 'php1';//此處并不會(huì)在頁(yè)面中輸出
$a = ob_get_level();
$b = ob_get_contents();//獲得緩存結(jié)果,賦予變量
ob_clean();
 
ob_start();
echo 'php2';//此處并不會(huì)在頁(yè)面中輸出
$c = ob_get_level();
$d = ob_get_contents();//獲得緩存結(jié)果,賦予變量
ob_clean();
 
ob_start();
echo 'php3';//此處并不會(huì)在頁(yè)面中輸出
$e = ob_get_level();
$f = ob_get_contents();//獲得緩存結(jié)果,賦予變量
ob_clean();
 
echo 'level:'.$a.',ouput:'.$b.'<br>';
echo 'level:'.$c.',ouput:'.$d.'<br>';
echo 'level:'.$e.',ouput:'.$f.'<br>';

結(jié)果如下:

復(fù)制代碼 代碼如下:

level:1,ouput:php1
level:2,ouput:php2
level:3,ouput:php3

當(dāng)然,當(dāng)你關(guān)閉某個(gè)級(jí)別的緩沖,如下測(cè)試:

復(fù)制代碼 代碼如下:

ob_end_clean();
 
ob_start();
echo 'php1';
$a = ob_get_level();
$b = ob_get_contents();
ob_clean();
 
ob_start();
echo 'php2';
$c = ob_get_level();
$d = ob_get_contents();
ob_end_clean();  //清空緩存并關(guān)閉緩存
 
ob_start();
echo 'php3';
$e = ob_get_level();
$f = ob_get_contents();
ob_clean();
 
echo 'level:'.$a.',ouput:'.$b.'<br>';
echo 'level:'.$c.',ouput:'.$d.'<br>';
echo 'level:'.$e.',ouput:'.$f.'<br>';

結(jié)果如下:

復(fù)制代碼 代碼如下:

level:1,ouput:php1
level:2,ouput:php2
level:2,ouput:php3

相關(guān)文章

最新評(píng)論