The output control function does not affect the file header information sent using header() or setcookie(), only the data blocks similar to echo() and PHP code.
Let’s take a simple example first to give everyone a general impression of Output Control:
Example 1.
Copy code The code is as follows :
ob_start(); //Open buffer
echo "Hellon"; //Output
header("location:index.php") ; //Redirect the browser to index.php
ob_end_flush(); //Output all content to the browser
?>
Everyone who knows the header() function Everyone knows that this function will send a file header to the browser, but if there is any output before using this function (including empty output, such as spaces, carriage returns, and line feeds), an error will be prompted. If we remove ob_start() in the first line and then execute this program, we will find that we get an error message: "Header had all ready send by"! But with ob_start, there will be no error message. The reason is that when the buffer is opened, the characters after echo will not be output to the browser, but will be retained on the server. They will not be output until you use flush or ob_end_flush, so it will not Any file header output errors!
1. Introduction to related functions:
1. Flush: refresh the contents of the buffer and output.
Function format: flush()
Description: This function is frequently used and is very efficient.
2. ob_start: Open the output buffer
Function format: void ob_start(void)
Description: When the buffer is activated, all non-file header information from the PHP program will not be sent, but saved. in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
3. ob_get_contents: Returns the contents of the internal buffer.
Usage: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.
4. ob_get_length: Returns the length of the internal buffer.
Usage: int ob_get_length(void)
Description: This function will return the length in the current buffer; the same as ob_get_contents, if the output buffer is not activated. then returns FALSE.
5. ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.
Usage: 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 but delete it !
7. ob_implicit_flush: Turn on or off absolute refresh
Usage method: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $|=x, this string can be turned on /Close the buffer, and the ob_implicit_flush function is the same as that. The default is to close the buffer. After turning on absolute output, each script output is sent directly to the browser, and there is no need to call flush()
2. In-depth understanding:
1. About the Flush function:
This function appeared in PHP3. It is a very efficient function. It has a very useful function of refreshing the browser's cache. Let's give a very obvious operation effect. Example to illustrate flush.
Example 2.
Copy code The code is as follows:
for($i = 1; $i <= 300; $i++ ) print(" ");
// This sentence is very critical. The structure of the cache makes its content available from the browser only when it reaches a certain size. Output inside
// In other words, if the cache content does not reach a certain size, it will not be output before the program execution is completed. After
// testing, I found that the bottom limit of this size is 256 characters long. This means that the content received by the cache in the future will
// be sent out continuously.
For($j = 1; $j <= 20; $j++) {
echo $j."
";
flush(); //This will add something to the cache The content is squeezed out and displayed on the browser
sleep(1); //Let the program "sleep" for a second, which will allow you to see the effect more clearly
}
?>
http://www.bkjia.com/PHPjc/320445.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320445.htmlTechArticleThe output control function does not affect the file header information sent using header() or setcookie(), only those Data blocks similar to echo() and PHP code work. Let’s give a simple example first...