In-depth understanding of ob function buffer mechanism and phpob mechanism in php _ PHP Tutorial

Source: Internet
Author: User
In-depth understanding of ob function buffer mechanism and phpob mechanism in php. The ob function buffer mechanism in php is deeply understood, and the phpob mechanism is deeply understood. the following section describes the ob function buffer mechanism in php and shows the code analysis in the form of text instructions: in-depth understanding of the ob function buffer mechanism in php and the phpob mechanism

In php, the ob function buffer mechanism is shown as follows through text instructions and code analysis:

For a php programmer who just got started, the php buffer is almost transparent. In their mindEcho print_r functionThe data will be displayed in the browser with a click. 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.

Let's talk about another role in this article, ob function, ob isOutput_buffering. Since the ob function is a php extension function, the main operation of the ob function isPhp buffer.

Simply put, we must return to the topic at the beginning of this article,Echo print_rHow does the data output by the function arrive at the browser for users to see? The actual process is as follows:

The code is as follows:
Echo, print_r => php output_buffering => webServer buffer => browser display

We can clearly see fromEcho, print_rThe function sends messages to the client through two buffers, while the client also goes through a browser buffer. What we will discuss in this article is: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 additionOutput_buffering in php. iniIn cli mode, php 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. The buffer zone outputs data in a timely manner based on instructions. 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

The code is as follows:
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.

The code is as follows:
Ob_get_contents ()

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

The code is as follows:
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 outOb_get_contents ()And then callOb_clean ()Clear the buffer 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 laterOb_clean (), There will naturally be no output. Call the ob_flush () function before the ob_clean operation to achieve the expected effect.

The code is as follows:
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, I want to explain it again.Apache buffer flush ()Working principle: inApache moduleIn sapi, flush will callSapi_module ()Using apache api: ap_rflush to refresh apache output buffer. Of course, other apache modules suchMod_gzipThe result of this action may be changed, and the output buffer may be performed by the user. this will cause the results produced by the flush () function not to be immediately sent to the client browser.

The code is as follows:
Ob_get_clean ()

If you are familiarOb_get_contents ()AndOb_clean (),Then this function is very simple. 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.

The above content is an in-depth understanding of the ob function buffer mechanism in php. I hope it will be helpful for your future study.

The following section describes the ob function buffer mechanism in php through text instructions and code analysis...

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.