In the process of using PHP to use the header and Setcookie two functions, these two functions will send a file header information to the browser, but if the two functions before the use of any output (including null output, such as space, carriage return and line break) will prompt an error, The message is as follows: "Header had all ready to send by"! What is the way to send the file header information in the case of output? In PHP 4.0 added a buffer control of several functions, using these functions can help us solve a lot of problems. 
Introduction of related functions: 
1. Flush: Outputs the contents of the buffer and deletes the buffer. function format: Flush () Description: This function is often used and is highly efficient. 
2, Ob_start: Open output buffer function format: void Ob_start (void) Description: When the buffer is activated, 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. 
3. Ob_get_contents: Returns the contents of the internal buffer. Usage: string ob_get_contents (void) Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated. 
4. Ob_get_length: Returns the length of the internal buffer. Usage: int ob_get_length (void) Description: This function returns the length in the current buffer, as in ob_get_contents if the output buffer is not active. FALSE is returned. 
5. Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer. Method of Use: 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 use method: void Ob_end_clean (void) Description: This function does not output the contents of the internal buffer! 
7. Ob_implicit_flush: Turn absolute refresh on or off using the method: void Ob_implicit_flush ([int flag]) Description: People who have used Perl know the meaning of $|=x, this string can open/close the buffer, and ob_ The Implicit_flush function, like that, defaults to closing the buffer and opening the absolute output. 
Ii. Examples of Use: 
In the beginning, the author said that using buffer control function can prevent the file hair send information error, here is an example: 
 
 
-------------------------------------------------------------         <?   PHP prompt         ob_start ();   Open buffer         echo   "hello/n";   Output         header (' location:gotourl.php ');       redirect the browser to gotourl.php         ?>         -------------------------------------------------------------
 
If you remove ob_start,php, you will be prompted to make an error in the 4th line of the file (the error message is as shown earlier), but with Ob_start, there is no hint of an error, because when the buffer is opened, the character behind the echo is not output to the browser, but remains on the server. Until you use flush or ob_end_flush will not output, so there will be no file header output error!
Here is a very classic use:
For example you use <?phpinfo ();? > Get the server and client settings information, but this information will be different from the client, if you want to save the Phpinfo () function output what to do? Before there is no buffer control, it can be said that there is no way, but with the control of the buffer, we can easily solve:
 
-------------------------------------------------------------         <?     Ob_start ();   Open buffer         phpinfo ();   Use the Phpinfo function         $info =ob_get_contents ();   The contents of the buffer area are obtained and assigned to $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, you can save the Phpinfo information of different users, which in the past I am afraid there is no way to do! In fact, the above is a few "process" into the "function" Method!
 
The above is PHP in the control of the buffer code example of the content of the detailed, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!