Using php ob functions to understand the buffer mechanism, phpob

Source: Internet
Author: User

Using php ob functions to understand the buffer mechanism, phpob

For a php programmer who just got started, the php buffer is almost transparent. In their minds, an echo print_r function, the data will fly to the browser and display it. I have always thought so simply. In fact, in the world of technology, it has always been from simple to complex. Maybe those technical developers start simply like you and me, but in the face of the cruel reality, they have to adjust their strategies, in order to improve the machine running efficiency, I finally thought of the idea that impressed us.

When it comes to buffering, that is, buffer, it must be compared with the cache. it is meaningless to simply compare the definition. It is not worth looking at what they do. The cache solves the problem of how to quickly find and use data to save cpu consumption, while the cache solves the problem of mismatch between high-speed cpu and low-speed I/O devices.

Next, let's talk about another role in this article. The ob function is short for output_buffering. Since the ob function is a php Extension function, the ob FunctionMainThe operation is php buffer.

Simply put, we have to return to the topic at the beginning. How does the data output by the echo print_r function reach the browser for users to see? The actual process is as follows:

Echo, print_r => php output_buffering => webServer buffer => browser display

  We can clearly see that from echo and print_r functions to sending information to the client has gone through two buffers, while the client has gone through a browser buffer. This article mainly discusses php output_buffering.

  Usage of the buffer when the ob function is not used

Most of our code does not use ob functions at all. Are they using buffers? This depends on the php settings. The buffer zone is controlled by the output_buffering variable in php. ini. The default value is off. You can set it to on to enable the buffer. After the buffer is called, even if the ob function is not used in the program, the Code actually uses the buffer. In addition, regardless of the output_buffering settings in php. ini, php in cli mode is always disabled by default.

Why is it a buffer? Simply put, the high-speed cpu can process its own data early and want to pass the data to the user through the line. However, the line is too narrow and the next transmission cannot pass. If a buffer zone is introduced, the cpu can quickly put the generated data into the buffer zone, and then rest where you are. Buffer accordingCommandOutput Data in due time. This reasonably resolves the conflict between high-speed cpu and low-speed I/O devices.

When will the data in the buffer zone be output? 1. When the buffer zone is full, the buffer has a capacity. When the buffer reaches the limit, the content is automatically output. 2. The script has been executed. A lot of small programs do not output so much content. You can't wait until the buffer is full to output it again ~ This is naturally not the case.

  Buffer usage when using ob Functions

  Ob_start () 

Enable the output buffer. This function is one of the most called functions. When output_buffering is set to on or x k, this function expands the output buffer to a large value instead of opening the output buffer. Of course, when output_buffering is set to off, ob_start will enable the buffer. Ob_start () can also pass an optional parameter output_callback function, which is described in the php official manual.

Ob_get_contents ()

  Only get the content of the output buffer, but do not clear it.

  Ob_end_clean () and ob_clean ()

The differences between these two functions can be seen literally. The former clears and closes the buffer content, and the latter only clears the content. Note that using these two functions, echo, print_r, and other functions will not output content.

I tried to print the content of ob_get_contents () through print_r, and then call ob_clean () to clear the buffer, so as to avoid subsequent operations on the buffer and frequent failures. Think about it, the content of print_r is written into the buffer again, and the ob_clean () operation is performed later, naturally there will be no output. Call the ob_flush () function before the ob_clean operation to achieve the expected effect.

Ob_flush () and flush ()

Ob_flush () sends the buffer content and discards it. Therefore, it is best to use ob_get_contents () to obtain the buffer content before this function. Flush () clears the Server Buffer and sends it to the client. Therefore, in the process, the ob_flush () should be called before the flush function.

In addition, the working principle of Apache buffer flush () is described below: In the sapi of apache module, flush will indirectly use apache api by calling the flush member function pointer of sapi_module :: ap_rflush refreshes apache output buffer. Of course, other modules of apache, such as mod_gzip, may change the result of this action, and may output the buffer themselves. This will cause the results produced by the flush () function to not be immediately sent to the client browser.

 Ob_get_clean ()

  If you are familiar with ob_get_contents () and ob_clean (), this function is easy. Because it is a combination of the first two. It mainly obtains the content of the current buffer and deletes the current output buffer.

 

There are still many ob functions, but most of them are simple to use and easy to understand. You can refer to the php manual for detailed explanations. This article lists some functions that I didn't quite understand at the beginning. Of course, there will be new problems in the future. If you want to think about the problems and solve them, you may have fun in your life.

  

 

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.