The following is the following to show the ob function buffering mechanism in PHP through text description and code analysis:
For a newly-earned php programmer, the php buffer is almost transparent. In their minds, aecho print_r function, the data will fly to the browser with a "whoosh" sound and be displayed. I have always thought so simply. In fact, in the world of technology, it has always been from simple to complex. Perhaps those technology developers began to be as simple as you and me, but in the face of cruel reality, they had to adjust their strategies to improve the efficiency of the machine operation, and finally thought of those ideas that made us admire.
Speaking of buffering, that is, buffers, we must compare them with caches. It is meaningless to simply compare definitions, so it is better to see what they do. Caching solves the problem of how to quickly find and utilize data and save CPU consumption, while buffering solves the problem of mismatch between high-speed CPU and low-speed I/O devices.
Let’s talk about another protagonist in this article, ob function, ob isoutput_bufferingabbreviation of . Since the ob function is a php extension function, the main operation of the ob function isphp bufferNow.
After briefly speaking, we must return to the theme at the beginning.echo print_rHow does the data output by the function reach the browser for users to see? The actual process is as follows:
echo、print_r=>php output_buffering=>webServer buffer=>browser buffer=>browser display
We can clearly see fromecho、print_rThe function has experienced two buffers to send information to the client, and the client has also experienced a browser buffer. What we mainly discuss in this article isphp output_buffering。
The usage of buffers when the ob function is not used
Many times our code does not use ob functions at all, so do they use buffers? It depends on the php setting. The buffer is controlled by the output_buffering variable in it. The default value is off, which can be set to on to open the buffer. After calling the buffer, even if the ob function is not used in the program, the code actually uses a buffer. Also, don't worryoutput_buffering, PHP in cli mode is always turned off by default.
Why the buffer? Simply put, the high-speed CPU processed its own data early and wanted to pass it to the user through the line, but the line was too narrow and could not be transmitted at once. If a buffer is introduced, the CPU can quickly put the generated data into the buffer, and then stay here and rest. The buffer outputs data in a timely manner according to the instructions. This will reasonably resolve the contradiction between high-speed CPU and low-speed I/O devices.
When will the buffer data be output? 1. When the buffer is full, the buffer has a capacity size, and the content will be automatically output when it reaches the limit. 2. The script is executed. Many mini programs do not output so much content, so you can’t wait until the buffer is full before outputting~ This is very natural.
Use of buffers when using ob function
ob_start()
Turn on output buffering. This function is one of the most called functions. With output_buffering set to on or x k, this function is more like expanding the output buffer to a large extent than opening the output buffer. Of course, under the condition that 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, and the official php manual has detailed instructions.
ob_get_contents()
Just get the contents of the output buffer, but don't clear it.
ob_end_clean() and ob_clean()
The difference between these two functions can be seen from the literal meaning. The former clears the buffer content and closes it, while the latter just does the cleanup work. It should be noted that if these two functions are used, the previous functions such as echo, print_r will not output the content.
The author once tried to print it out via print_rob_get_contents()and then call the content ofob_clean()Clear the buffer to avoid affecting the subsequent operation of the buffer and failing repeatedly. Think about it carefully, the content of print_r is written to the buffer again, and then it is done.ob_clean()There will naturally be no output for the operation. Calling the ob_flush() function before the ob_clean operation can achieve the expected effect.
ob_flush() and flush()
ob_flush() sends out the contents of the buffer and discards the contents. Therefore, it is best to use ob_get_contents() to obtain the buffer content before this function. flush() flush out the server-side buffer and send it to the client. Therefore, from a process perspective, you should call ob_flush() first and then call the flush function.
Also explain it againApache buffer flush()How it works:apache moduleUnder the sapi, flush will be called bysapi_module()The flush member function pointer indirectly uses apache's api::ap_rflush to refresh the output buffer of apache. Of course, other apache modules are likemod_gzipIt may change the result of this action, and may perform the output buffer by yourself, which will cause the result generated by the flush() function to not be sent to the client browser immediately.
ob_get_clean()
If you're already proficientob_get_contents()andob_clean(),Then this function is very simple. Because it is a combination of the first two. It mainly gets the contents of the current buffer and deletes the current output buffer.
There are many ob functions, but most of them are simpler to use and easier to understand. You can refer to the php manual, which will provide detailed explanations. This article lists some functions that I didn’t understand very well at the beginning. Of course, new problems will arise in the future. If you think of problems and solve them, the joy of life may be here.
The above content is a deep understanding of the ob function buffering mechanism in PHP. I hope it will be helpful to everyone's future learning.