Output buffering in PHP

Source: Internet
Author: User


In PHP, when running Echo,print, the output is not immediately transmitted via TCP to the client browser, but instead writes the data to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before the TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process will send the output data from PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written to these places echo/pring. PHP Buffer---browser


There are three functions related to refreshing buffering in PHP:

1). Flush
Refreshes the PHP program's buffering, regardless of the circumstances in which PHP is running. This function sends the full output of the program so far to the user's browser. However, the function does not have any effect on the cache mode of the server or client browser, nor does it have any effect on the cache of PHP itself.

2). Ob_flush

This function outputs the cache of PHP itself. The cache for PHP itself is controlled by output_buffering in php.ini. The function of Ob_flush () is to remove the contents of the original output cache, set to wait for the output state, but not directly to the client, then you need to use Ob_flush () and then flush (), the client talent immediately get the output of the script.

        Two PHP configurations related to PHP output buffering are:
           1: output_buffering   : on/off  or integer   . When set to on, output cache control is used in all scripts and does not limit the size of the cache. When set to an integer, such as output_buffering=4096, when the cached data reaches 4096 bytes, it will voluntarily output a flush cache. The difference in this parameter is the reason why the above code will run differently at different times. When Output_buffering is turned off, the entire output of the script (ECHO) is instantly sent to the client, which outputs a number per second when running the above code. When Output_buffering is turned on, the output is slow to exist on the server until the end of the script is sent to the client.
          2: Implicit_flush : On/off. Setting on means that when the script has output, it will be sent to the client on its own initiative. It is equivalent to adding flush () after Echo.


3). Ob_implicit_flush

This function forces the output to be sent to the browser whenever there is an output. This does not require each output (echo) to be sent to the browser with flush ().

Sample Example


  1. <?php
  2. Ob_end_clean ();
  3. Echo Str_pad ( " ", 256);  
  4. for &NBSP; ( $i =100;&NBSP; $i >0;&NBSP; $i --)   {   
  5. echo $i, ' <br/>'  ;  
  6. flush ( );  
  7. sleep (1);  
  8. }  
  9. ?>
The above code should output $i once every second.The purpose of the above Echo Str_pad ("", 256) is that IE needs to accept 256 bytes before it starts to display. The above code also has the following two kinds of wording.
  1. <?php
  2. Echo Str_pad ( " ", 256);  
  3. for &NBSP; ( $i =100;&NBSP; $i >0;&NBSP; $i --)   {   
  4. echo $i, '<br  /> ';  
  5. Ob_flush ( ); Sometimes it's just not possible to flush.
  6. flush ( );  
  7. sleep (1);  
  8. }  
  9. ?>

View Plain
  1. <?php
  2. Ob_implicit_flush (TRUE);
  3. echo &NBSP; str_pad ( " &NBSP; " ,&NBSP; );    
  4. for &NBSP; ( $i =100;&NBSP; $i >0;&NBSP; $i --)   {   
  5. echo $i, '<br  /> ';  
  6. Ob_flush ( );
  7. sleep (1);  
  8. }  
  9. ?>

We also need to note that the refresh buffer is not only affected by the above aspects, but also affected by the following:

1). Individual webserver programs, especially the Webserver program under Win32, will still cache the output of the script until the end of the program, before sending the result to the browser. Some Apache modules, such as Mod_gzip, may make their own output caches, which will cause the flush () function to produce results that are not immediately sent to the client browser. Even the browser will cache the received content before it is displayed. For example, the Netscape browser caches content before it accepts the start of a newline or HTML tag, and does not display the entire table until the </table> tag is accepted. Some version numbers of Microsoft Internet Explorer only start to display the page after accepting 256 bytes, so you must send some extra spaces to let these browsers display the page content.


Here is a very easy piece of code



<?php/*--------------------Write your own cache class---------------*/class my_cache{//define the variables private $cache _time;// Cache valid time private $cache _file;//cache file Save path//initialization class, by default index.html time is 1function __construct ($cache _file= ' index.html ', $cache _time= "1") {$this->cache_file= $cache _file; $this->cache_time= $cache _time;} Cache starts function Cache_start () {if ($this->cache_active) {include ($this->cache_file); exit;} Turn on cache Ob_start ();} Infer if the cache file exists and can use function cache_active () {//infer if the file exists if (file_exists ($this->cache_file)) {[email protected] ( $this->cache_file);//Gets the last modified time//whether the inferred time is available if ($this->cache_time< $last _time) {//available, including a direct display of return true;} else{//Delete the cache, once again establish the cache unlink ($this->cache_file); return false;}} Make cache folder generation function Cache_creat () {//without inference directly generate cache file folder and file, loop generate file $file=explode ("/", $this->cache_file); $num = Count ($file) -1;for ($i =0; $i < $num; $i + +) {$tm. = $file [$i]. " /"; if (!file_exists ($TM)) {mkdir ($TM);}}} The cached output function cache_end () {$cache _content=ob_get_contents (); $this->cache_creat (); Email protected] ($This->cache_file, "w+"); Fwrite ($fp, $cache _content); Ob_end_flush ();} The cached purge function Cache_clean () {if (unlink ($this->cache_file)) {return true;} else {$this->alert ("Cache delete failed! Please check if the cache file exists "); return false;}} Defines a reminder function for a cached file functions alert ($a) {echo "<script>alert (' $a ');</script>";}}? > test page test.php<?include ' cache_my_class.php '; $my _cache=new My_cache ("./chunge/ge/hao/index.html", 5); $my _ Cache->cache_start (); At the beginning of the page-------page output $like = "I love oranges and bananas!" "; echo $like. " <br> "; $my _cache->cache_end ();//FINAL output


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.