In-depth understanding of the OB function buffering mechanism in PHP, PHPOB mechanism in-depth understanding of _php tutorials

Source: Internet
Author: User

In-depth understanding of the OB function buffering mechanism in PHP, PHPOB mechanism in-depth understanding


The following is a description of the OB function buffer mechanism in PHP in the form of text and code analysis to show you the following:

For a newly-started PHP programmer, the PHP buffer is almost transparent. In their mind, an echo print_r function , the data will be ' swish ' a sound to fly to the browser, displayed. I have always been so simple to think. In fact, in the world of technology, has always been from simple to complex, perhaps those technical developers began to be just like you, but in the face of the brutal reality, have to adjust the strategy, in order to improve the efficiency of machine operation, finally think of those who let us praise the idea.

When it comes to buffering, which is buffer, it has to be compared with the cache, and simply comparing the definitions is meaningless, as if to see what they do. The cache solves the problem of how to quickly find the exploit data and save CPU consumption, while the buffer solves the issue that the high-speed CPU does not match the low-speed I/O device.

Another main character of this article, OB function, OB is a shorthand for output_buffering . Since the OB function is a php extension function, the main operation of the OB function is php buffer .

Simply finish the two main characters of this article, we must also return to the beginning of the topic,echo print_r function output data is how to reach the browser to let users see it? The actual course is this:

Copy the Code code as follows:
Echo, print_r=>php output_buffering=>webserver buffer=>browser buffer=>browser Display

We can clearly see that from Echo, Print_r function to sending information to the client through two buffers, the client also experienced a browser buffer. Our main discussion in this paper is php output_buffering.

Use of buffers when no OB function is used

Our code often does not use the OB function at all, then do they use buffers? This depends on the PHP setup situation. The buffer is controlled by the output_buffering variable in the php.ini. The default value is off, which can be set to on to open buffer. After hitting buffer, even if the program does not use the OB function, the code is actually using the buffer. In addition, PHP is always turned off by default in CLI mode, regardless of the output_buffering settings in php.ini .

Why, what if the buffer zone? In short, the high-speed CPU processing their own data early, want to pass the line to the user, but the line is too narrow, the delivery is not over. If a buffer is introduced, the CPU can quickly put the generated data into a buffer, and then where it is cool where to stay and rest. Buffers output data in a timely manner according to the instructions. This kind of a reasonable solution to the high-speed CPU and low-speed I/O equipment contradictions.

When does the buffer data output? 1, when the buffer is full, the buffer is a capacity, the limit will be automatically output content. 2, the script execution is complete. A lot of small program output content is not so much, always can not wait until the buffer full and then output it ~ this is natural.

  Use of the buffer when using the OB function

Copy the Code code as follows:
Ob_start ()

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

Copy the Code code as follows:
Ob_get_contents ()

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

Copy the Code code as follows:
Ob_end_clean () and Ob_clean ()

These two functions can be distinguished by their literal meanings. The former clears the buffer contents and closes, and the latter simply does the cleanup work. It is important to note that these two functions are used, and the functions such as Echo, Print_r, and so on are used before to output the content.

I have 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 after the face buffer, repeatedly failed. When you think about it, Print_r's content is written to the buffer again, and then the Ob_clean () operation is done, and there is no output. The desired effect is achieved by calling the Ob_flush () function before the Ob_clean operation.

Copy the 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 obtain buffer content before this function. Flush () brushes the server-side buffer and sends it to the client. Thus, from a process, you should call Ob_flush () before calling the flush function.

Also explains how Apache buffer Flush () works: Under Apache module SAPI, flush will be called by Sapi_module () Flush member function pointer that indirectly uses 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 the output buffer, which will cause the flush () function results will not be immediately sent to the client browser.

Copy the 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 a combination of the former two. It basically gets the contents of the current buffer and deletes the current output buffer.

There are many OB functions, but most of them are relatively simple and easy to understand. You can refer to the PHP manual, there will be a detailed explanation. This article lists some of the functions that the author does not understand at first, and of course, there will be new problems in the future, think of problems and solve problems, the joy of life may be here.

The above content is this article for PHP in the OB function buffer mechanism in-depth understanding, I hope that everyone in the future to learn some help.

http://www.bkjia.com/PHPjc/1042691.html www.bkjia.com true http://www.bkjia.com/PHPjc/1042691.html techarticle in PHP, the OB function buffer mechanism in-depth understanding, PHPOB mechanism in-depth understanding of the following in PHP in the OB function buffer mechanism through the text description plus code analysis of the form to show you as follows: For a just ...

  • Related Article

    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.