In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces php how to close the buffer, the article is very detailed, has a certain reference value, interested friends must read!
php Close buffer method: 1, open PHP installation directory, find and open the configuration file "php.ini";2, in the configuration file, find the "output_buffering" item, set the value of the item to "Off" can be.
Operating environment of this tutorial: Windows 7 system, PHP 7.1 version, DELL G3 computer
PHP buffer
Script output information is first placed in the buffer, and only when the buffer is full or the script runs out, the data will be transferred to the next stage. The output_buffering setting can be found in the configuration file php.ini, my default configuration (Windows/XAMPP v3.2.2) is 4096
If set to On, buffers are available and have no size limit;
If Off, buffers are unavailable;
If set to integer, buffer available and set size in bytes
It should be noted that ini_set cannot set the buffer size. When initializing the script running environment, the buffer has been defined; in cli mode, output_buffering is always Off by default.
So the question is, what's the difference between a closed buffer and an open buffer? What does the initial buffer size do? CLI mode is off by default. Can it be turned on if required by business? What are the benefits of cache existence?
1. What's the difference between buffer closed and buffer open?
Pass Code Check
Run code when output_buffering is configured to 4096
Output result: After waiting for 3 seconds, start and end are output at the same time
Run the same code when Output_buffering is configured to Off
Output result: output start first, wait 3 seconds later, output end
A single look at the results are exactly the same ~ but observing its operation process found a very surprising difference. If the buffer is off, output "start" first, wait 3 seconds and then output "end". Buffering is enabled, waiting for script execution to complete, and outputting the result
In this example, flush is used to flush the apache buffer, similar to making the PHP buffer directly contact the browser so that our attention can focus on the PHP buffer itself.
By way of example, PHP buffers are like a big house, and each time an output function such as echo is executed, the data is thrown into this big house until the script is executed or the ob_flush function is executed, releasing the data from the big house and throwing it into the WebServer buffer. After closing the PHP buffer, the echo function is executed. There is no big house to put it. It has to be handed over directly to the WebServer buffer.
Through examples and code execution results, we can understand that the so-called PHP buffer is the data storage hub opened up by PHP itself, and a series of ob functions are for this operation. (A few examples of functions)
ob_clean(); //Delete the contents of the internal buffer without closing the buffer (no output).
ob_end_clean(); //Delete the contents of the internal buffer, close the buffer (no output).
ob_get_contents(); //Returns the contents of the buffer, no output
ob_get_length(); //Returns the length of the internal buffer, or FALSE if the buffer is not activated.
2. What does the initial buffer size do?
Change output_buffering from 4096 to 5 to facilitate testing
execute code
Output Description: Wait 3 seconds, output 1234end
execute code
Output Description: Output 12345, wait 3 seconds, output end
By comparing the outputs, it is possible to explain the difference.
Buffer can only put 5 bytes of data, put in less than this maximum value, but also can be stored first, if greater than or equal to the threshold value, then we have to kick out the data, this is like we manually executed ob_flush function an effect.
3. CLI mode is off by default. Can it be turned on if required by business?
This problem can be supplemented. In non-cli mode, if php.ini sets the buffer to be closed, but the business needs to use the buffer, how to fix it, it is difficult to change the configuration. Of course, it's not that hard to do. At this time, another ob function comes.
ob_start(); //Opens an output buffer where all output information is no longer sent directly to the browser.
Knowing the concept of PHP buffer similar to temporary data storage, go back and look at the TP5 source code.
Let's look at the ob_get_level and ob_get_clean functions first
ob_get_level(): Returns the nesting level of the output buffering mechanism.
ob_get_clean(): Gets the contents of the current buffer and deletes the current output buffer.
From the code can also know one or two, if the level is greater than 0, get the contents of the buffer and delete the buffer, and then open a new one through ob_start, which is also in line with the "re-application" described in the comments.
Here explain the so-called nesting, as for when the value of level will be 0, and what is non-zero, please close php.ini output_buffering, output to see the result. In addition, multiple ob_start, multiple ob_get_level, try to compare the output results.
Let's look at two models.
When ob_start opens a new buffer multiple times, the data structure is constructed as the second type, which is nested. ob_get_level returns the nested level.
Personal understanding, nesting can be applied to Java flow mechanism to analogy, one buffer of data output to the next buffer, and then can do customized operations on the data.
Now look at TP5 source code, the idea is relatively clear. If the buffer is initialized, delete it and reopen it because it may have a size limit. The size of the buffer opened by executing ob_start is "large enough", but the exact size is not specified for the time being. We only know that no matter how big the data is written, it will not be sent until the end of the script.
4. What are the benefits of having a cache?
PHP output data is sent to WebServer, if echo is executed once, this increases resource consumption, it is better to store it in memory, and it is more efficient to send it uniformly.
Web requests contain HTTP headers, which cannot have any output until they are set. PHP buffers do this for us, outputting various data before sending the header. The buffer will send the header information to WebServer first, and then send the data message uniformly.
The above is "php how to close the buffer" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.