PHP Output Buffer _php tutorial

Source: Internet
Author: User

Output buffers for PHP

What is a buffer?

Simply put, the function of the buffer is to put the input or output of the content into the memory, and not display or read. As to why there is a buffer, this is a very broad problem, if interested, you can find the information in the net Mountain.
In fact, the most essential role of the buffer is to coordinate the operation of high-speed CPUs and relatively slow IO devices (disks, etc.).

Where is PHP useful to buffer when it executes?

To understand the PHP buffer, it is necessary to know where the buffer is set when PHP is executed.
When PHP is executed, if it encounters the code of the output data such as Echo Print_r, PHP will put the output data into the buffer of PHP itself, waiting for the output.
When the buffer of PHP itself is instructed to output the contents of the buffer, the data in the buffer will be output to Apache, Apache receives the data from the PHP output, and then the data is present in the Apache buffer area until the output
When Apache receives the instruction, it simply outputs the contents of the buffer and returns the contents of the buffer to the browser.

This shows that when PHP is going to output data, it will go through two buffers (first of its own, then Apache) and back to the browser.

What role does the buffer play in PHP?

1. The most common is that some data has been output before using the header function, which results in some errors, such as cannot modify header information–headers already sent by;

12 echo "This is test" header (

This error occurs because some data has been output before the header, and when the data is output, Apache will send a response state to the browser (since the output, that is, the request is valid), and then you use the header function again
Send HTTP headers, this error will be returned, the error means: HTTP header has been sent out, you can not modify him again.
Why use buffers to avoid this error?
because the header function is not affected by the buffer, when a header function is encountered, PHP immediately executes Apache send this HTTP header browser.
While the output data PHP opens the output buffer, the data will be stored in the buffer, waiting for output. This avoids the errors that occurred earlier.
2. When you write a file to download the program via PHP.
in order to make the file download more secure and more controllable, many friends like to use PHP to write files to download the page. The principle is simple, that is, through the fwrite to read and display the contents of the file, and then through the header to send HTTP headers, so that the browser know that this is an attachment, so that The
can achieve the effect of providing the download.
If you use the above method to provide download page, there will be an efficiency problem, if a file is large, assuming that 100M, then without opening the buffer output, you must read all the 100M data, and then return to the page at once, if you do so, the user will read all the data
And then get a response that reduces the user experience.
If the output buffer is turned on, when the PHP program reads a section of the file and then prints it out to Apache immediately, and then allows Apache to return to the browser immediately, this reduces the user wait time. What about the data behind that? We can write a while loop, Read the file for a period of time
reads it out at a time, until the file is fully output, so that the browser can continue to receive data without waiting for all files to be read.

In addition, this approach solves another serious problem. For example, a file is 100M, if the buffer is not turned on, you need to read all the 100M files into memory, and then output. But what if the PHP program makes a memory limit? In order to ensure the stability of the server, Administrators usually put PHP's execution
Memory set a limit (by PHP.ini total Memory_limit, the default value is 8M), that is, each PHP program uses memory can not use more than this value of memory. Assuming the value is 8M, and the file to be read is 100M, There is simply not enough memory to read the file. At this point, we need to use the
method above to solve this problem, each time read only one paragraph, so that the memory limit can be avoided
3. Static file caching
Now a lot of companies have such a demand, is a page in the first visit, will execute PHP, and then return the displayed content to the browser, but also need to save this display to the server, so the next time you visit, the file is saved directly on the server directly displayed, and do not need to do the operation of PHP
This is called "Static page Caching". So how do you get the content back to the browser while saving the data to the server? This will use the output buffer.

123456 ob_start();echo'aaa';$string= ob_get_contents();file_put_contents('a.html',$string);ob_flush();flush();

Configuration related to output buffers

In php.ini, there are two configuration items that are closely related to a buffer
This configuration directly affects the buffer of PHP itself, there are 3 configuration parameters. ON/OFF/XK (x is an integer value);
On-Open buffer
Off-Close buffer
256k-The buffer is opened, and when the buffer content exceeds 256k, the buffer is automatically refreshed (sending the data to Apache);

This configuration directly affects the Apache buffer, which has 2 configuration parameters. On/Off
On-automatically refreshes the Apache buffer, that is, when PHP sends data to the Apache buffer, there is no need to wait for other instructions, to directly return the output to the browser
Off-does not automatically flush Apache buffers, wait for refresh instructions after receiving data

Buffer-related functions

function and Implicit_flush, whether the Apache buffer is refreshed automatically
The function is to send an instruction to Apache and let Apache flush its output buffers.
Opens the output buffer, regardless of how the php.ini file is configured, and if the function is used, the output buffer is opened even if output_buffering is set to OFF
The Ob_start function also accepts a parameter, which is a callback for a function, which means that the contents of the buffer need to be processed once, and then placed in the buffer, before the contents of the input buffer are passed in.
Instructs PHP itself to flush its own buffer and send the data to Apache
Clears the contents of the PHP buffer
Clears the contents of the PHP buffer and closes the output buffer
Sends the contents of PHP's own buffer to Apache and clears the contents of its own buffer.
Clears the buffer after it gets the contents of the buffer.
Get the content in the output buffer
Gets the contents of the buffer and sends the content to Apache
Gets the length of the content in the buffer
Gets the name of the function that is being recalled when running Ob_start, for example:
Ob_start (' Ob_gzhandler ');
Print_r (ob_list_handlers);
will print out ob_gzhandler;
The function acts as a callback parameter for the Ob_start, which is called before the buffer refreshes to either gzip or deflate the data. This function requires the support of the zlib extension.

Working with buffer related content

The order relationship of 1.ob_flush and flush. The above analysis shows that Ob_flush is related to PHP itself, while the flush operation is Apache buffer, all of which we need to execute ob_flush when we use these two functions.
Execute flush again, because we need to send the data from PHP to Apache and then back to the browser by Apache. If PHP has not yet flushed the data to Apache, it calls Flush, and Apache has no data to return to the browser.

2. Some browsers, if the number of characters received is too small, it will not show the data, such as the old version of IE (must be more than 256k to display). This will cause a doubt, obviously in PHP and Apache have done a flush buffer operation, but the browser does not appear the data they want, Maybe that's what this is all about. So when testing, you can add more than one space after the output data to fill up the data to make sure that the browser does not cause such strange problems.

3. Some webserver, his own output buffer will have some restrictions, such as nginx, he has a configuration fastcgi_buffer_size 4k, that is, when the content of its output buffer to 4K will be refreshed, so in order to guarantee the content of the data, You can add the following code to guarantee the content length

12345 echo str_repeat(" ",4096); ?>

4. In Apache, if you turn on the Mod_gzip compression module, this may cause your flush function to be unsuccessful, because Mod_gzip has its own output buffer, and when PHP executes the flush function, it instructs Apache to flush the output buffer, However, the content needs to be compressed, Apache will output the content to its own Mod_gzip module, Mod_gzip also has its own output buffer, he will not immediately output, so that the content can not be output immediately. To improve the situation, you can close the Mod_gzip module, or add the following to httpd.conf to suppress compression

1 SetEnv no-gzip dont-vary true techarticle what is a buffer for the output buffer of PHP? Simply put, the function of the buffer is to put the input or output content into memory instead of displaying or reading. As to why there is a buffer ...

  • 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: 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.