PHP buffer output_buffering Using the detailed _php tips

Source: Internet
Author: User
Tags flush
Buffer
Buffer is a memory address space, the Linux system default size is generally 4096 (4KB), that is, a memory page. It is primarily used for data transfer between devices that are not synchronized at the storage speed or between devices with different priority levels. By using buffer, the process can wait for each other to get smaller. Here's a popular example, when you open a text editor to edit a file, you input a character, the operating system will not immediately write this character directly to the disk, but first write to the buffer, when the full of a buffer, the data in the buffer will be written to disk, Of course, when calling kernel function flush (), it is mandatory to write the dirty data in the buffer back to disk.

Similarly, when executing echo,print, the output is not immediately transmitted via TCP to the client browser, but writes the data to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process passes the output data from the PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written in sequence in these places:echo/print-> php buffer-> TCP buffer-> browser

PHP output_buffering
By default, PHP buffer is turned on, and the buffer default is 4096, or 4KB. You can find the output_buffering configuration in the php.ini configuration file. When Echo,print output user data, the output data will be written to the PHP output_buffering until Output_ The buffering is full, sending the data over TCP to the browser display. You can also manually activate the PHP output_buffering mechanism by Ob_start (), so that even if the output exceeds 4KB data, it does not really send the data to TCP to the browser, because Ob_start () the PHP buffer space set to large enough. The data is sent to the client browser only until the end of the script, or when the Ob_end_flush function is invoked.

1. When output_buffering=4096, and output less data (less than a buffer)

Copy Code code as follows:

<?php
for ($i = 0; $i < $i + +) {
Echo $i. ' <br/> ';
Sleep ($i + 1); //
}
?>

Phenomenon: Not every few seconds there will be intermittent output, but until the response to the end, to see the output at a time, waiting for the server script processing end, the browser interface remains blank. This is because the amount of data is too small, PHP output_buffering is not full. The order in which the data is written, followed by echo->php buffer->tcp Buffer->browser

2. When output_buffering=0, and output less data (less than a buffer)

Copy Code code as follows:

<?php
Through Ini_set (' output_buffering ', 0) does not take effect
Should edit/etc/php.ini, set output_buffering=0 disable output buffering mechanism
Ini_set (' output_buffering ', 0); Disable the output buffering feature completely
for ($i = 0; $i < $i + +) {
Echo $i. ' <br/> ';
Flush (); Notify the bottom of the operating system to send data to the client browser as soon as possible
Sleep ($i + 1); //
}
?>

Phenomenon: Not consistent with the previous display, disabling the PHP buffering mechanism, in the browser can be intermittent to see the intermittent output, do not have to wait until the script finished to see the output. This is because the data is not stuck in the PHP output_buffering. The order in which the data is written is, in turn, Echo->tcp buffer->browser

3. When output_buffering=4096., the output data is greater than a buffer and does not call Ob_start ()

Copy Code code as follows:

#//Create a file of 4KB size
$dd If=/dev/zero of=f4096 bs=4096 count=1
<?php
for ($i = 0; $i < $i + +) {
Echo file_get_contents ('./f4096 '). $i. ' <br/> ';
Sleep ($i + 1);
}
?>

Phenomenon: The response is not finished (HTTP connection is not closed), intermittent output can be seen intermittently, the browser interface will not remain blank. Although the PHP output_buffering mechanism is enabled, it will still be intermittent output, rather than one-time output, because output_buffering space is not enough. Each full PHP buffering, the data is sent to the client browser.

4. When output_buffering=4096, the output data is greater than a TCP buffer, call Ob_start ()

Copy Code code as follows:

<?php
Ob_start (); Open PHP Buffer
for ($i = 0; $i < $i + +) {
Echo file_get_contents ('./f4096 '). $i. ' <br/> ';
Sleep ($i + 1);
}
Ob_end_flush ();
?>

Phenomenon: Until the server-side script processing completed, the response ended, only to see the complete loss, the output interval time is very short, so that you do not feel a pause. Before the output, the browser maintains a blank interface, waiting for server-side data. This is because once PHP calls the Ob_start () function, it expands the PHP buffer to be large enough until the Ob_end_flush function call or the script runs a node to send data from PHP buffer to the client browser.

Output buffering function

1.ob_get_level
Returns the nesting level of the output buffering mechanism to prevent the template from nesting itself repeatedly.

1.ob_start
The activates the output_buffering mechanism. Once activated, the script output is no longer directed to the browser, but is temporarily written to the PHP buffer memory area.

PHP opens the output_buffering mechanism by default, except that by invoking the Ob_start () function, the output_buffering value is extended to large enough. You can also specify $chunk_size to specify the value of Output_buffering. The default value for $chunk _size is 0, which means that the data in PHP buffer will not be sent to the browser until the script is finished running. If you set the size of the $chunk_size, the data in buffer will be sent to the browser as long as the length of the data in the buffer reaches the value.

Of course, you can process the data in the buffer by specifying $ouput_callback. For example, function Ob_gzhandler, compressed data in the buffer and then sent to the browser.

2.ob_get_contents
Gets a copy of the data in the PHP buffer. It is worth noting that you should call the function before the Ob_end_clean () function is called, otherwise ob_get_contents () returns an empty character. The two functions,

3.ob_end_flush and Ob_end_clean
, are somewhat similar, shutting down the ouptu_buffering mechanism. But the difference is that Ob_end_flush simply flushes the data in the PHP buffer (flush/send) to the client browser, while Ob_clean_clean empties the data in the PHP bufeer (erase), but does not send it to the client browser. After the Ob_end_flush call, the data in the PHP buffer still exists, and ob_get_contents () can still get a copy of the data in the PHP buffer. After the Ob_end_clean () call Ob_get_contents () takes an empty string, and the browser does not receive the output, that is, there is no output.

idiomatic cases
often see Ob_start () used in some template engines and in the paging file cache. The program code that loads the template in the following wet CI:

Copy Code code as follows:

<span style= "White-space:pre" > </span>/*
* Buffer the output
*
* We buffer The output for two reasons:
* 1. Speed. Get a significant speed boost.
* 2. So this final rendered template can be
* Post-processed by the output class. Why do we
* Need post processing? For one thing.
* Show the elapsed page load time. Unless we
* Can intercept the content right before it's sent to
* The browser and then stop timer it won ' t be accurate.
*/
Ob_start ();
If The PHP installation does not support short tags we ' ll
Do a little string replacement, changing the short tags
To standard PHP echo statements.
if ((bool) @ini_get (' short_open_tag ') = = = FALSE and Config_item (' rewrite_short_tags ') = = TRUE)
{
Replace short Mark <?=***>
echo eval ('?> '. Preg_replace ("/;*\s*\?>/", ";?>", Str_replace (' <?= ', ' <?php echo ', file_get_contents ($ _ci_path)));
}
Else
{
Include ($_ci_path); Include () vs include_once () allows for multiple views with the same name
}

Logging Debug Information
Log_message (' Debug ', ' File loaded: '. $_ci_path);
Return the file data if requested
if ($_ci_return = = TRUE)
{
$buffer = Ob_get_contents ();
@ob_end_clean ();
return $buffer;
}
/*
* Flush the buffer ... or buff the flusher?
*
* In order to permit the views to be nested within
* Other views, we are need to flush the content back out whenever
* We are beyond the ' a ' of output buffering so
* It can be seen and included properly by the The
* template and any subsequent ones. oy!
*
*/
if (Ob_get_level () > $this->_ci_ob_level + 1)
{
Ob_end_flush ();
}
Else
{
Adding template content to the output stream
$_ci_ci->output->append_output (Ob_get_contents ());
Clear Buffer
@ob_end_clean ();
}

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.