In the process of using PHP, it is unavoidable to use the header and Setcookie two functions, these two functions send a piece of file header information to the browser, but if you have any output (including null output, such as a space, carriage return, and line feed) before using the two functions, you will be prompted with an error. The prompt information is as follows: "Header had all ready send by"! What is the way to send header information under the circumstances of the output? Several functions of buffer control are added to PHP 4.0, and these functions can help us solve many problems.
 
Introduction of related functions:
 
1, Flush: The contents of the output buffer and delete the buffer.
 
function format: Flush ()
 
Description: This function is frequently used and highly efficient.
 
2, Ob_start: Open the output buffer
 
function format: void Ob_start (void)
 
Note: When the buffer is activated, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. To output the contents of the buffer, you can use Ob_end_flush () or use Ob_end_clean () to output the contents of the buffer.
 
3, Ob_get_contents: Return the contents of the internal buffer.
 
Using method: String ob_get_contents (void)
 
Description: This function returns the contents of the current buffer and FALSE if the output buffer is not active.
 
4, Ob_get_length: Returns the length of the internal buffer.
 
How to use: int ob_get_length (void)
 
Description: This function returns the length of the current buffer, as ob_get_contents if the output buffer is not active. Returns FALSE.
 
5, Ob_end_flush: Send the contents of the internal buffer to the browser, and turn off the output buffer.
 
Usage method: void Ob_end_flush (void)
 
Description: This function sends the contents of the output buffer (if any).
 
6, Ob_end_clean: Delete the contents of the internal buffer, and close the internal buffer
 
Usage method: void Ob_end_clean (void)
 
Description: This function will not output the contents of the internal buffer!
 
7. Ob_implicit_flush: Turn absolute refresh on or off
 
How to use: void Ob_implicit_flush ([int flag])
 
Description: Anyone who has ever used Perl knows? The meaning of $|=x, this string can open/close the buffer, and the Ob_implicit_flush function is the same as that, the default is to turn off the buffer and turn on absolute output.
 
Second, the use of examples:
 
In the beginning, the author said that the use of buffer control functions can prevent file hair to send information error, the following is an example:
 
 
 
  
  Copy Code code as follows: 
 
 
  
 
  
  
? PHP prompt 
  
Ob_start (); Open buffer 
  
echo "hello/n"; Output 
  
Header (' location:gotourl.php '); redirect browser to gotourl.php 
  
?> 
  
 
 
 
  
 
If you remove ob_start,php, you will be prompted to make an error on line 4th of the file (the error message is shown above), but with Ob_start, you will not be prompted for an error, because when the buffer is open, the characters behind Echo are not exported to the browser, but remain on the server. Until you use flush or ob_end_flush will not output, so there will be no file header output error!
 
The following is a classic use:
 
For example, you use <?phpinfo (); > Get Server and client setup information, but this information will vary depending on the client, what if you want to save the output of the phpinfo () function? Before there is no buffer control, we can say that there is no way, but with the control of the buffer, we can easily solve:
 
 
 
  
  Copy Code code as follows: 
 
 
  
 
  
  
? 
  
Ob_start (); Open buffer 
  
Phpinfo (); Using the Phpinfo function 
  
? $info =ob_get_contents (); Get the contents of the buffer area and assign it to the $info 
  
? $file =fopen (' Info.txt ', ' W '); Open File Info.txt 
  
Fwrite (? $file,? $info); Write information to Info.txt 
  
Fclose (? $file); Close File Info.txt 
  
?> 
  
 
 
 
  
 
Using the above method, can be different users of the Phpinfo information to save, which in the past I am afraid to do! In fact, the above is a number of "process" into the "function" Method!