When using PHP, you may need to use the header and setcookie functions. These two functions will send a file header to the browser, however, if you have any output (including empty output, such as space, carriage return, and line feed) before using these two functions, an error will be prompted. The message is as follows: "Headerhadallreadysendby "! There is any way you can use the header and setcookie functions when using PHP. These two functions will send a file header to the browser, however, if you have any output (including empty output, such as space, carriage return, and line feed) before using these two functions, an error is prompted, and the message is as follows: "Header had all ready send "! Is there any way to send the file header information when output occurs? Several buffer control functions are added to PHP 4.0. using these functions can help us solve many problems.
1. related functions:
1. Flush: output the content in the buffer and delete the buffer. Function format: flush () Description: This function is frequently used and highly efficient.
2. ob_start: format of the function used to open the output buffer: void ob_start (void) description: When the buffer is activated, all non-file header information from the PHP program is not sent, but saved in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or ob_end_clean () to output the buffer content.
3. ob_get_contents: returns the content of the internal buffer. Usage: string ob_get_contents (void) Description: This function returns the content in the current buffer. if the output buffer is not activated, FALSE is returned.
4. ob_get_length: return the length of the internal buffer. Usage: int ob_get_length (void) Description: This function returns the length of the current buffer. it is the same as ob_get_contents if the output buffer is not activated. Returns FALSE.
5. ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer. Usage: void ob_end_flush (void) Description: This function sends the content of the output buffer (if any ).
6. ob_end_clean: delete the content of the internal buffer and disable the use of the internal buffer: void ob_end_clean (void) Description: This function will not output the content of the internal buffer!
7. ob_implicit_flush: How to enable or disable absolute refresh: void ob_implicit_flush ([int flag]) description: Perl users all know the meaning of $ | = x, this string can enable/disable the buffer, while the ob_implicit_flush function is the same as that. by default, the buffer is disabled and absolute output is enabled.
II. example:
At the beginning, I mentioned that using the buffer control function can prevent file header sending errors. The following is an example:
-------------------------------------------------------------
-------------------------------------------------------------
If ob_start is removed, PHP will prompt an error in row 4th of the file (the error information is shown in the previous figure). However, when ob_start is added, no error will be prompted because when the buffer zone is enabled, the characters after echo are not output to the browser, but are retained on the server until you use flush or ob_end_flush. therefore, no file header output error occurs!
The following provides a classic purpose:
For example The configuration information of the server and client is obtained, but the information varies with the client. what if I want to save the output of the phpinfo () function? There is no way to control the buffer, but with the control of the buffer, we can easily solve the problem:
-------------------------------------------------------------
-------------------------------------------------------------
With the above method, you can save the phpinfo information of different users. I am afraid there is no way to do this before! In fact, the above is how to convert some "procedures" into "functions!
The above is a detailed description of the code instance for buffer control in PHP. For more information, see PHP Chinese network (www.php1.cn )!