In PHP, when echo and print are executed, the output is not immediately transmitted to the client browser for display through tcp, but the data is written to the php buffer. The php output_buffering mechanism means that a new queue is established before the tcp buffer, and data must pass through the queue. When a php buffer is full, the script process will hand over the output data in the php buffer to the system kernel and pass it to the browser via TCP for display. Therefore, the data will be written to these places in sequence echo/pring -> php buffer -> tcp buffer -> browser
There are three functions related to refresh buffering in PHP:
1).flush
Flushes the PHP program's buffer regardless of the circumstances under which PHP is executed. This function sends all the program's output so far to the user's browser. But this function will not have any impact on the caching mode of the server or client browser, nor will it have any impact on the caching of PHP itself.
2).ob_flush
This function outputs the cache of PHP itself. PHP's own caching is controlled by output_buffering in php.ini. The function of ob_flush() is to take out the content originally stored in the output cache and set it to the waiting output state, but it will not be sent directly to the client. In this case, you need to use ob_flush() first and then flush(), the client to get the output of the script immediately.
3).ob_implicit_flush
This function forces output to be sent to the browser immediately whenever there is output. In this way, there is no need to use flush() to send it to the browser after each output (echo).
Example
1). Some web server programs, especially web server programs under Win32, will still cache the output of the script until the end of the program before sending the results to the browser. Some Apache modules, such as mod_gzip, may perform output caching themselves, which will cause the results of the flush() function to not be sent to the client browser immediately. Even browsers cache received content before displaying it. For example, the Netscape browser will Cache content before the beginning of the tag, and after receiving Before marking, the entire table will not be displayed. Some versions of Microsoft Internet Explorer will only begin to display the page after receiving 256 bytes, so some extra spaces must be sent for these browsers to display the page content.
The following is a very simple piece of code
<?php /*--------------------编写自己的缓存类---------------*/ class my_cache{ //定义有关变量 private $cache_time;//缓存有效时间 private $cache_file;//缓存文件保存路径 //初始化类,默认是index.html时间是1 function __construct($cache_file='index.html',$cache_time="1"){ $this->cache_file=$cache_file; $this->cache_time=$cache_time; } //缓存开始 function cache_start(){ if ($this->cache_active){ include($this->cache_file); exit; } //开启缓存 ob_start(); } //判断缓存文件是否存在并且可用 function cache_active(){ //判断文件是否存在 if(file_exists($this->cache_file)){ $last_time=@filemtime($this->cache_file);//获取最后修改时间 //判断时间是否可用 if($this->cache_time<$last_time){ //可用,包含进来直接显示 return true; }else{ //删除该缓存,重新建立缓存 unlink($this->cache_file); return false; } } } //进行缓存目录的生成 function cache_creat(){ //不用判断直接生成缓存文件目录及文件,循环生成文件 $file=explode("/", $this->cache_file); $num=count($file)-1; for ($i=0;$i<$num;$i++){ $tm.=$file[$i]."/"; if (!file_exists($tm)){ mkdir($tm); } } } //缓存的输出 function cache_end(){ $cache_content=ob_get_contents(); $this->cache_creat(); $fp=@fopen($this->cache_file, "w+"); fwrite($fp, $cache_content); ob_end_flush(); } //缓存的清除 function cache_clean(){ if(unlink($this->cache_file)){ return true; }else { $this->alert("缓存删除失败!请检查缓存文件是否存在"); return false; } } //定义缓存文件的提醒函数 function alert($a){ echo "<script>alert('$a');</script>"; } } ?> 测试页面test.php <? include 'cache_my_class.php'; $my_cache=new my_cache("./chunge/ge/hao/index.html",5); $my_cache->cache_start(); //在页面的最开始 -------页面输出 $like="我爱吃橘子香蕉!"; echo $like."<br>"; $my_cache->cache_end();//最后进行输出