Buffer control in PHP

Source: Internet
Author: User
Tags html header
PHP4.0 provides a set of output buffer functions. The output buffer allows you to write the function to compress the buffer. The output buffer in PHP4 supports storing HTML header information, regardless of whether the HTML body is output or not. However, in PHP, header information (header (), contenttype, andcookies) is not buffered. In the process of using PHP, it is inevitable to use h PHP4.0 to provide an output buffer function set. The output buffer allows you to write the function to compress the buffer. The output buffer in PHP4 supports storing HTML header information, regardless of whether the HTML body is output or not. However, in PHP, header information (header (), content type, and cookies) is not buffered.

When using PHP, the header and setcookie functions are used. These two functions 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 "!. Several buffer control functions are added to PHP 4.0. using these functions can help us solve many problems.
 

Function name function format function description
Flush flush () outputs the content in the buffer and deletes the buffer. This function is frequently used and highly efficient.
Ob_start void ob_start (void) to open the output buffer. When the buffer zone 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.
Ob_get_contents string ob_get_contents (void) returns the content of the internal buffer. This function returns the content in the current buffer. if the output buffer is not activated, FALSE is returned.
Ob_get_length int ob_get_length (void) returns the length of the internal buffer. 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.
Ob_end_flush void ob_end_flush (void) sends the content of the internal buffer to the browser and closes the output buffer. This function sends the content of the output buffer (if any ).
Ob_end_clean void ob_end_clean (void) deletes the content of the internal buffer and closes the internal buffer. This function will not output the content of the internal buffer!
Ob_implicit_flush void ob_implicit_flush ([int flag]) anyone who turns on or off absolute refresh and uses Perl knows the meaning of $ | = x. This string can enable/disable the buffer zone, the ob_implicit_flush function is the same as that. by default, the buffer is disabled and absolute output is enabled.

II. instance analysis:

1. Use a buffer-controlled function to prevent information sending errors in the file header.

Ob_start (); // open the buffer
Echo "Welcome/n"; // output
Header ("location: next. php"); // redirects the browser to next. php
?>

If ob_start is removed, PHP will prompt an error in row 4th of the file. the error message is "Header had all ready send by". However, when ob_start is added, no error will be prompted, the reason is that when the buffer is opened, the characters after echo will not be output to the browser, but will be kept in the server's buffer until you use flush or ob_end_flush, so there will be no output errors in the file header!

2. Save the output (this is a classic use ).

If you want to know the screen output information of the client, such as the output result of the function, and the output information varies with the client. We can use functions Obtain the settings of the server. 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 it.

Ob_start (); // open the buffer
Phpinfo (); // use the phpinfo function
$ Info = ob_get_contents (); // Obtain the buffer content and assign it to $ info
Export file1_fopen('phpinfo.txt ', 'w'); // open the phpinfo.txt file.
Fwrite ($ file, $ info); // write the information to phpinfo.txt
Fclose ($ file); // Close the phpinfo.txt file
?>

With the above method, you can save the phpinfo information of different users. I am afraid there is no way to do this before! Similarly, the buffer method can be used to save tasks that are difficult to complete in general methods. In fact, this is to convert some "procedures" into "functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.