In-depth understanding of ob function buffer mechanism and phpob mechanism in php
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:
Copy codeThe 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
Copy codeThe 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.
Copy codeThe Code is as follows:
Ob_get_contents ()
Only get the content of the output buffer, but do not clear it.
Copy codeThe 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.
Copy codeThe 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.
Copy codeThe 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.