Buffer is a memory address space, the Linux system default size is generally 4096 (4KB). It is primarily used to store data between devices that are not synchronized, or between devices with different priority levels.
Buffer allows for fewer mutual waits between processes.
For example, when you open a text editor and enter a character, the system does not immediately write to the disk. Instead, it is stored in buffer, and the contents of the buffer are written to disk when the buffer is full.
Of course, you can also use flush to force data from buffer to be written to disk.
In PHP, such as Echo,print, the output is not immediately passed through TCP to the browser output. Instead, the data is written to PHP buffer. When a PHP buffer is full, it is passed to the browser via TCP.
Echo/print, PHP output_buffer, TCP buffer, browser
PHP output_buffering
By default, PHP buffer is turned on, and the default value is 4096 (4KB). The output_buffering configuration can be found in the php.ini. Know that buffer is full before it is sent to the browser.
You can also set Ob_start () so that even exceeding the default value (4KB) is not immediately sent to the browser, only until the script is finished, or the Ob_end_flush method is called, before it is output to the browser.
1.output_buffering=4096, output less data (less than one buffer)
<?phpfor ($i =0; $i <10; $i + +) { echo $i. ' <br> '; Sleep (2);}? >
Run result: After all scripts have been run, the output will not be completed because the data is not full of a buffer size.
2.output_buffering=4096, output less data (less than one buffer), close output_buffering, modify PHP.ini output_buffering=0
<?phpfor ($i =0; $i <10; $i + +) { echo $i. Str_repeat (" ", 500). ' <br> '; When you receive 256 (or more) bytes to start displaying the page, you must send some extra spaces to let these browsers display the contents of the page. flush (); Sleep (1);}? >
Running results: Because PHP buffering is disabled, you do not have to wait until the script is finished to output, the data does not stay in PHP buffer, you can see intermittent output intermittently. echo-TCP buffer->browser
3. When output_buffering=4096, output larger data (greater than one buffer), do not use Ob_start ()
<?phpfor ($i =0; $i <10; $i + +) { echo file_get_contents (' F.txt '). $i. ' <br> '; Sleep (1);}? >
Run Result: F.txt is a file greater than 4KB, because the buffer space is larger than the buffer default value, and will be output whenever a buffer is full, so you can see the intermittent output.
4. When output_buffering=4096, output larger data (greater than one buffer), use Ob_start ()
<?phpob_start (); for ($i =0; $i <10; $i + +) { echo file_get_contents (' F.txt '). $i. ' <br> '; Sleep (1);}? >
Run Result: Because Ob_start () is used, the buffer is set to a sufficient amount of space, so it will be saved until the script finishes executing.
Output_buffering method
1.ob_start
Activating the output_buffering mechanism, once activated, the script is no longer output directly to the browser, but is temporarily written to the PHP buffering area. It is not sent until after the script has finished running.
2.ob_get_contents
Get the data in PHP buffering, note: To be called before Ob_end_clean (), otherwise only the null character will be obtained.
3.ob_end_flush and Ob_end_clean
Ob_end_flush will output the data in PHP buffering, but it will not be emptied.
Ob_end_clean will not output, only the data in PHP buffering will be emptied.
Note:
Ob_flush/flush's description in the manual, is to refresh the output buffer, and also need to be used, so it will cause many people confused ...In fact, the two of them are different objects, in some cases, flush does not do anything at all .The ob_* series function is the output buffer that operates PHP itself.So, Ob_flush is the buffer that refreshes PHP itself.Flush, strictly speaking, this is only useful when PHP is installed as an Apache module (handler or filter). It is a buffer that refreshes webserver (which can be considered to be Apache).under Apache module's SAPI, flush will invoke Sapi_module's flush member function pointer indirectly, calling Apache's Api:ap_rflush to flush Apache's output buffer, and of course the manual says, There are some Apache other modules that may change the result of this action:some Apache modules, such as Mod_gzip, may make their own output caches, which will result in the flush () function producing results that are not immediately sent to the client browser. even the browser will cache the received content before it is displayed. For example, the Netscape browser caches content before it accepts the start of a newline or HTML tag, and does not display the entire table until the </table> tag is accepted. Some versions of Microsoft Internet Explorer do not start to display the page until 256 bytes are accepted, so you must send some extra spaces to let these browsers display the page content.
So, the correct use of the two is the order. Ob_flush first, then flush.
Of course, under other sapi, do not call flush also can, only to ensure that your code portability, recommended for use.
This article explains how to use PHP output_buffering cache, more relevant content please focus on the PHP Chinese web.
Related recommendations:
How to implement Bigpipe chunked output via PHP
How to use Fsockopen get/post to submit forms and upload files via PHP
Describes the PHP filter HTML tag attribute class related content