Examples of PHP output buffering and its applications

Source: Internet
Author: User
Tags flush sleep first row

Buffering (buffer) is a technique used to coordinate data transfer between devices with very different throughput speeds, and the area used to hold buffered data is called a buffer, and in computer science, a buffer is used to temporarily store data when data is transferred from one place to another. One technique that is similar to buffering is caching, which is designed to solve the problem of different data storage and transmission speeds, and the buffer is primarily used in writing, while caching is primarily used for reading.

As shown above, is a simple buffer simulation diagram, the data of the left-side entry has a single input volume, speed, quantity, but the right side output data with large volume, slow speed characteristics. If there is no buffer, it is easy to cause data congestion, after the buffer, when the data fills the buffer, and then unified output, can greatly reduce the burden of the system.

PHP in the process of execution, embedded HTML code, ' echo ', ' print_r ' and other instructions are a data output, it is because of the existence of buffer, the system can be executed in PHP after the data sent to the browser, run the following code:

<?php
echo "Here is the first row of data";
echo "Here is the second row of data, sleep below 5 seconds";
Sleep (5);
echo "Here is the third line of data, the following is the HTML code";
?>

Discover that the browser is displaying all of the content at the same time instead of first and second rows of data, wait 5 seconds before displaying the following data. Not only that, the PHP buffer also provides us with more powerful features that we can capture, change, and so on before the data is sent. PHP provides us with "Ob_" series functions, such as the following code, which can be substituted for some characters:

<?php
Ob_start ();
Echo "Hello world, this is http://www.111cn.net/";
$content = Ob_get_contents ();
Ob_end_clean ();
Echo str_replace ("http://", "https://", $content);
?

The Ob_start,ob_get_contents,ob_end_clean above are used to open user buffers, get cached content, and close the cache, and all output control functions in PHP are 1:
flush-Refresh output system buffers
Ob_ clean-empty (Erase) output buffer
ob_end_clean-empty (erase) buffer and turn off output buffer
ob_end_flush-flush out (send out) output buffer content and turn off buffer
ob_flush-flush out (send out) output buffer The content in the zone
ob_get_clean-gets the contents of the current buffer and deletes the current output slow.
Ob_get_contents-Returns the contents of the output buffer
ob_get_flush-the contents of the buffer, returns the contents as a string, and closes the output buffer.
Ob_get_length-Returns the length of the output buffer content
Ob_get_level-Returns the nesting level of the output buffer
ob_get_status-Gets the status of all output buffers
ob_gzhandler-in OB A callback function used in _start to compress the contents of the output buffer. Ob_start callback function to gzip output buffer
ob_implicit_flush-Open/close absolute brush
ob_list_handlers-lists all the output handlers in use.
ob_start-turn on output control buffering
output_add_rewrite_var-add URL rewrite value (add URL rewriter values)
output_reset_rewrite_vars- Resets the value of the URL rewrite (reset URL rewriter values)

In PHP, you can set the cache by PHP.ini's output_buffering, on represents infinity, off is off, the number indicates the size of the buffer (in bytes), the default size is 4KB, and if set to OFF, Is the code for example one that can be segmented in the browser? The answer is no, there are two points to note, the 1th even if the PHP cache is closed, PHP output at the system level also has a cache (can be understood as a Linux system stdout cache), must pass through the Flush function output; The 2nd is some browsers have restrictions on the length of text received at once, If too few, it is not displayed. So the code can be segmented to show:

<?php
echo "Here is the first row of data";
echo str_repeat ("", 1024);
echo "Here is the second row of data, sleep below 5 seconds";
Flush ();
Sleep (5);
echo "Here is the third line of data, the following is the HTML code";
?>
Of course, in the actual production environment, the direct closure of the output_buffering is relatively rare, we can operate through the Ob_ series functions, the following example is the use of buffering server push (Comet) example. The following code can be used to push information to the client:

<?php
Ob_start ();
$i = 0;
while ($i <100) {
echo $i. Str_repeat ("", 2024);
$i + +;
Ob_flush ();
Flush ();
Sleep (5);
}
?>

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.