In PHP, the OB function buffering mechanism deeply understands _php instance

Source: Internet
Author: User

In PHP, the OB function buffering mechanism is illustrated by the text description and code analysis to show the following:

For an entry-level PHP programmer, the PHP buffer is almost transparent. In their minds, an echo print_r function , the data will be ' swish ' to the browser, display. I have always been so naïve to think. In fact, in the technology world, has always been from simple to complex, perhaps those technology developers start simple as you and I, but in the face of brutal reality, have to adjust the strategy in order to improve the efficiency of the machine, and finally think of those let us praise idea.

When it comes to buffering, that is, buffer, there must be a comparison with the cache, and it is meaningless to simply compare the definitions, and look at what they do. Caching solves the problem of how to quickly find the use of data and save CPU consumption, while buffering solves the mismatch between the high speed CPU and the low-speed I/O device.

Again, the OB function, OB, is the second leading actor in this article, which is a shorthand for output_buffering . Since the OB function is a php extension function, then the main operation of the OB function is php buffer .

Simply finish the two protagonists, we must return to the beginning of the theme, theecho print_r function output data is how to reach the browser to let users see it? The actual course of events is this:

Copy Code code as follows:

Echo, print_r=>php output_buffering=>webserver buffer=>browser buffer=>browser Display

We can clearly see that from the Echo, Print_r function to send information to the client experienced two buffers, the client also experienced a browser buffer. This article is mainly about php output_buffering.

Use of buffers when no OB function is used

Our code often doesn't use the OB function at all, so do they use a buffer? This depends on the PHP settings. The buffer is controlled by the output_buffering variable in the php.ini. The default value is off, which you can set to on to open the buffer. After the call to the buffer, even if the program does not use the OB function, the code is actually using the buffer. In addition, PHP is always closed by default in CLI mode, regardless of the output_buffering settings in php.ini .

Why would it be a buffer? In short, the high-speed CPU processing their own data early, want to pass through the line to the user, but the line is too narrow, the transmission is not past. If you introduce a buffer, the CPU can quickly put the generated data into a buffer, and then it's cool where you're going to rest. The buffer outputs data in a timely manner according to instructions. This sample is a reasonable solution to the high-speed CPU and low-speed I/O device contradictions.

What time does the data in the buffer output? 1, when the buffer is full, the buffer is of capacity size, to reach the limit will automatically output content. 2, script execution completed. A lot of small program output content is not so much, can not wait until the buffer full and then output it ~ this is more natural.

  Use of buffers when using the OB function

Copy Code code as follows:

Ob_start ()

Turn on output buffering. This function is one of the functions we call most. In the case where the output_buffering is set to On or X K, this function is not so much about opening the output buffer as extending the output buffer to a large size. Of course, when the output_buffering is set to OFF, the Ob_start will play the role of opening the buffer. Ob_start () can also pass an optional parameter Output_callback function, the PHP official manual is described in detail.

Copy Code code as follows:

Ob_get_contents ()

Just get the contents of the output buffer, but do not clear it.

Copy Code code as follows:

Ob_end_clean () and Ob_clean ()

The two functions can be seen in terms of the literal meaning of the difference. The former clears the buffer content and closes, and the latter is just doing cleanup work. It is important to note that the use of these two functions, the previous use of the Echo, Print_r and other functions do not output the content.

The author has tried to print out the contents of ob_get_contents () through Print_r, and then call Ob_clean () to clear the buffer, so as not to affect the operation of the buffer in the face of failure. Think carefully, print_r content is written to the buffer again, and after doing Ob_clean () operation, naturally there will be no output. Calling the Ob_flush () function before the Ob_clean operation can achieve the desired effect.

Copy Code code as follows:

Ob_flush () and flush ()

Ob_flush () sends out the contents of the buffer and discards the content. It is therefore preferable to use ob_get_contents () to get the buffer content before this function. Flush () brushes out the server-side buffers and sends them to the client. Therefore, from the process, it should be called First Ob_flush () and then call the Flush function.

Also explain how the Apache Buffer Flush () works: Under the SAPI of Apache module , flush will invoke Sapi_module () Flush member function pointer, indirectly using Apache's Api::ap_rflush to flush Apache's output buffer. Of course, Apache other modules such as mod_gzip may change the result of this action, may own output buffers, which will cause the results of the flush () function will not immediately be sent to the client browser.

Copy Code code as follows:

Ob_get_clean ()

If you have mastered ob_get_contents () and Ob_clean (), then this function is very simple. Because it is the combination of the first two. It basically gets the contents of the current buffer and deletes the current output buffer.

There are a lot of OB functions, but most of them are simpler and easier to understand. You can refer to the PHP manual, there will be a detailed explanation. This article lists some of the functions that I don't understand at first, and of course there will be new problems coming up, thinking of problems and solving problems, and the pleasures of life may be here.

The above content is in the PHP in the OB function buffer mechanism to understand deeply, hope for everyone to study in the future help.

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.