Code for buffer control in PHP _ PHP Tutorial

Source: Internet
Author: User
Code for buffer control in PHP. 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, before using these two functions, you may need to 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 ()

Note: This function is frequently used and highly efficient.

2. ob_start: Open the output buffer.

Function format: void ob_start (void)

Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored 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)

Note: 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)

Note: 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)

Note: 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 internal buffer.

Usage: void ob_end_clean (void)

Note: This function will not output the content of the internal buffer!

7. ob_implicit_flush: enable or disable absolute refresh

Usage: void ob_implicit_flush ([int flag])

Note: Anyone who has used Perl knows this? $ | = X indicates that 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 errors in sending information in the file header.
Example:

Ob_start (); // open the buffer
Echo "Hello/n"; // output
Header ('Location: gotourl. php'); // redirects the browser to gotourl. php
// Edit: www.jbxue.com
?>

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:

Ob_start (); // open the buffer
Phpinfo (); // use the phpinfo function
? $ Info = ob_get_contents (); // Obtain the buffer content and assign it? $ Info
? Using file1_fopen('info.txt ', 'w'); // open the info.txt file.
Fwrite (? $ File ,? $ Info); // write the information to info.txt
Fclose (? $ File); // Close the info.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! In fact, the above is how 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.