PHP output buffer and its application-php Tutorial

Source: Internet
Author: User
The PHP output buffer and its application buffer are used to coordinate data transmission between devices with large differences in throughput speed. the buffer zone is used to store buffered data, in computer science, when data is transferred from one place to another, the buffer zone is used to temporarily store data. A technology similar to caching is caching. They are designed to solve the problems caused by different data storage and transmission speeds. The difference is that caching is mainly used for writing, the cache is mainly used for reading.

For example, it is a simple buffer simulation chart. The data at the left-side entrance has the characteristics of small size, fast speed, and large quantity, but the output data at the right side is large in size and slow in speed. If there is no buffer, it is easy to cause data congestion. with the buffer, when the data fills up the buffer and then outputs the data in a unified manner, the burden on the system can be greatly reduced.

During PHP execution, the embedded HTML code, 'echo ', 'print _ R', and other commands are all output data at a time, precisely because of the existence of a buffer zone, the system can send the data to the browser again after php execution and run the following code:

 Title

It is found that the browser displays all the content at the same time, instead of displaying the first and second rows of data, and then displays the subsequent data after 5 seconds. In addition, the PHP buffer provides us with more powerful functions. we can capture and change the data before it is sent. PHP provides the "ob _" series of functions. for example, the following code can replace some characters:

 

In the above, ob_start, ob_get_contents, and ob_end_clean are used to enable the user buffer, obtain the cache content, and disable the cache area. all the output control functions in PHP include:

Flush-refresh the output system buffer

Ob_clean-clear (erase) output buffer

Ob_end_clean-clear (erase) the buffer and disable the output buffer.

Ob_end_flush-extracts (Sends) the output buffer content and closes the buffer.

Ob_flush-extract (SEND) the content in the output buffer

Ob_get_clean-get the content of the current buffer and delete the current output.

Ob_get_contents-return the content of the output buffer

Ob_get_flush-clears (Sends) the buffer content, returns the content in string format, and closes the output buffer.

Ob_get_length-return the length of the output buffer content

Ob_get_level-nesting level of the returned output buffer mechanism

Ob_get_status-obtains the status of all output buffers.

Ob_gzhandler-the callback function used in ob_start to compress the content in the output buffer. Ob_start callback function to gzip output buffer

Ob_implicit_flush-enable/disable absolute fl

Ob_list_handlers-list all output handlers in use.

Ob_start-enable the output control buffer

Output_add_rewrite_var-Add URL rewriter values)

Output_reset_rewrite_vars-Reset the value of the URL Rewrite (Reset URL rewriter values)

In php, you can use php. ini output_buffering to set the cache. On indicates infinity, Off indicates disabled, and number indicates the buffer size (in bytes). the default size is 4 kB. if it is set to off, can the code in example 1 be displayed in a browser in segments? The answer is No. There are two points to note: The first point is that even if the PHP Cache is disabled, php outputs are cached at the system level (which can be understood as the stdout cache in Linux ), it must be output through the flush function. The second point is that some browsers have limits on the length of the text received at a time. if it is too small, it will not be displayed. So this code can be displayed in segments:

 Title

Of course, in the actual production environment, it is rare to directly disable output_buffering. we can use ob _ series functions to perform operations. the following example uses the buffer for server push (comet). The following code pushes information to the client:

 
References
  1. Output Control function

  2. Questions about php_buffering

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.