PHP4.0 provides a collection of output buffering functions. Output buffering support allows you to write functions that wrap functions around compressed buffers. Output buffering support in PHP4 allows HTML header information to be stored regardless of whether the HTML body is output. But in PHP, header information ((header(), content type, and cookies) is not buffered.
In the process of using PHP, it is inevitable to use header and
setcookie two functions, these two functions will send a piece of file header information to the browser, but if there is any output (including empty output, such as spaces, carriage returns and line feeds) before using these two functions, an error will be prompted , the prompt information is as follows: "Header had all ready send by"! . Several functions for buffer control have been added to PHP 4.0. Using these functions can help us solve many problems.
Function name Function format Function description
Flush flush() outputs the contents of the buffer and deletes the buffer. This function is often used and is very efficient.
ob_start void ob_start(void) Open the output buffer. When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order 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.
ob_get_contents string ob_get_contents(void) Returns the contents of the internal buffer. This function returns the contents of the current buffer, or FALSE if the output buffer is not active.
ob_get_length int ob_get_length(void) Returns the length of the internal buffer. This function returns the length of the current buffer; like ob_get_contents, if the output buffer is not active. Returns FALSE.
ob_end_flush void ob_end_flush(void) Sends the contents of the internal buffer to the browser and closes the output buffer. This function sends the contents of the output buffer (if any).
ob_end_clean void ob_end_clean(void) Delete the contents of the internal buffer and close the internal buffer. This function does not output the contents of the internal buffer!
ob_implicit_flush void ob_implicit_flush ([int flag]) Turns absolute flush on or off. Anyone who has 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 close the buffer. area, turn on absolute output.
2. Example analysis:
1. Use buffer control functions to prevent errors in sending information in the file header.
<? //PHP prompt
ob_start(); //Open the buffer
echo "Welcome /n"; //output
header("location:next.php"); //Redirect the browser to next.php
?>
If ob_start is removed, PHP will prompt an error on line 4 of the file, and the error message is "Header had all ready send by". However, if ob_start is added, no error will be prompted. The reason is that when the buffer is opened, the error after the echo The characters will not be output to the browser, but will remain in the server's buffer. They will not be output until you use flush or ob_end_flush, so there will be no error that the file header has been output!
2. Save the output (this is a very classic use).
Suppose you want to know the client's screen output information, such as the output results of functions, etc., and this output information will differ depending on the client. We can use the function <?phpinfo(); ?> to get the server's setting information, but what if we want to save the output of the phpinfo() function? Before there was no buffer control, it can be said that there was no way at all, but with buffer control, we can easily solve it.
<?
ob_start(); //Open the buffer
phpinfo(); //Use phpinfo function
$info=ob_get_contents(); //Get the contents of the buffer and assign it to $info
$file=fopen('phpinfo.txt','w'); //Open the file phpinfo.txt
fwrite($file,$info); //Write information to phpinfo.txt
fclose($file); //Close the file phpinfo.txt
?>
Using the above method, you can save the phpinfo information of different users. This may not have been possible before! Similarly, the buffer method can be used to save tasks that are difficult to complete with ordinary methods. This is actually a method of converting some "processes" into "functions".