Home  >  Article  >  Backend Development  >  In-depth analysis of Php output buffering cache and program cache_PHP tutorial

In-depth analysis of Php output buffering cache and program cache_PHP tutorial

PHP中文网
PHP中文网Original
2016-07-21 15:00:421119browse

Test the ob cache and program cache below:
In order to make the test effect more obvious before testing, we first turn off the ob cache and set an obvious error level in php.ini.
Output_buffering=off
Display_errors=on
Code 1:

The code is as follows:

echo "php";
header("content-type:text/html;charset='utf-8'");
echo 'ok';


will appear
php
Warning: Cannot modify header information - headers already sent by (output started at D:wwwapachehtdocstestt2.php:2) in D:wwwapachehtdocstestt2.php on line 3
ok
Code 2:

The code is as follows:

ob_start();
 echo "php";
 header("content-type:text/html;charset='utf-8'");
 echo 'ok';


The result is completely correct

Cause analysis:
Code 1:
Php has already sent a header information to the browser when echo 'php',
when it appears again
header("content-type: text/html;charset='utf-8'");
I saw another header information. At this time, the above header information had been typed back. I couldn't change it and an error occurred.
Code 2: The ob cache is turned on. When echoing 'php', the data to be sent to the browser is first placed in the ob cache. Then, when a header information is encountered, it is also placed in the Ob cache. , when the page ends, press the http protocol to cache the program and return it to the browser.
For a deeper understanding, look at the following code
Code 3:

The code is as follows:

ob_start();
echo "php";
header("content-type:text/html;charset='utf-8'");
echo 'ok';
echo '
';
$ob=ob_get_contents();
echo $ob;


Yes Output


Ob_get_contents() just gets the contents of the ob cache without knowing them
Ob_get_contents( ) must be used before the ob cache is cleared.
Code 4:

The code is as follows:

ob_start(); 
echo "php"; 
ob_clean();//清除缓存内容但不关闭缓存区,还能用(往里添加东西) 
header("content-type:text/html;charset='utf-8'"); 
echo 'ok'; 
echo '
'; 
$ob=ob_get_contents(); 
echo $ob;


Result:


Code 5:

The code is as follows:

ob_start();
echo "php";
ob_end_clean();//清空缓存内容并关闭缓存区,ob_get_contents取不到内容
header("content-type:text/html;charset='utf-8'");
echo 'ok';echo '
';
$ob=ob_get_contents();
echo $ob;


Result:


Code 6:

The code is as follows:

ob_start();
echo "php";
ob_end_flush();//把缓存送到程序缓存内并关闭ob缓存
header("content-type:text/html;charset='utf-8'");
echo 'ok';
echo '
';
$ob=ob_get_contents();
echo $ob;


Code 7: Compare code 6 with ob_flush()

The code is as follows:

ob_start();
echo "php";
ob_flush();//把Ob 缓存送到程序缓存,不关闭ob缓存
header("content-type:text/html;charset='utf-8'");
echo 'ok';
echo '
';
$ob=ob_get_contents();
echo $ob;



Result:


Ob_clean()
Clear the ob cache content but do not close it
Ob_get_flush()
Flush the cache to the program cache, Turn off ob cache
Code 8:

The code is as follows:

Ob_start();
echo 'abc';
header("content-type:text/html;charset='utf-8'");
echo 'hello';
Ob_flush();
echo 'aa';
echo ob_get_contents();
//abchelloaaaa


2.ob_flush(),flush( ) and program cache
Code 9:

The code is as follows:

ob_start();
echo 'a';
flush();//把Ob缓存冲刷到程序缓存再冲刷到浏览器输出,不影响ob缓存
echo ob_get_contents();
//aa

Code 10:

The code is as follows:

ob_start();
echo 'a';
ob_flush();//把Ob缓存冲刷到程序缓存,ob里没有了缓存内容
echo "ob_con".ob_get_contents();
//a     是按正常输出的,Ob里没内容

Program cache:
Code 11:

The code is as follows:

echo str_repeat(" ",1024);//一些版本的 Microsoft Internet Explorer 只有当接受到的256个字节以后才开始显示该页面,
所以必须发送一些额外的空格来让这些浏览器显示页面内容。 
for($i=0;$i<5;$i++){
echo $i;
echo "
";
sleep(1);
flush();
}


It will output one number per second
If there is no flush(); it will temporarily store all the output in the program cache, and then return it to the browser as a whole after it is completed. This example illustrates the program cache.


http://www.bkjia.com/PHPjc/328041.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/328041.htmlTechArticle test below OB cache and program cache: In order to make the test effect more obvious before testing, we first turn off the OB cache and set an obvious error level in php.ini. Output_buffering=off Display_er...

The above is the in-depth analysis of Php output buffering cache and program cache_PHP tutorial. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn