Home > Article > Backend Development > what is php buffer?
What is a buffer?
Simply speaking, the function of a buffer is to put the input or output content into the memory first without displaying or reading it. As for why there is a buffer, this is a very If you are interested in a wide range of issues, you can find information on the website.
In fact, the most essential role of the buffer is to coordinate the operation of high-speed CPU and relatively slow IO devices (disks, etc.).
When PHP is executed, where is the buffer used?
If you want to understand the buffer of PHP, you need to know where the buffer is set when executing PHP.
When executing PHP, If you encounter code that outputs data such as echo print_r, PHP will put the data to be output into PHP's own buffer and wait for output.
When PHP's own buffer receives an instruction, it indicates that the buffer should be output. content, the data in the buffer will be output to apache, apache will receive the data output by PHP, and then store the data in apache's own buffer, and wait until the output
When apache receives the instruction, Just when you want to output the contents of the buffer, the contents of the buffer will be output and returned to the browser.
It can be seen that when PHP wants to output data, it will pass through two buffers (first its own , and then apache), and then return to the browser.
What role does the buffer play in PHP?
1. The most common thing is that before using the header function, a certain Some data, this will lead to certain errors, such as Cannot modify header information – headers already sent by;
The reason for this error is that some data has been output before the header, and while outputting these data , apache will send a response status to the browser at the same time (since there is output, that is, the request is valid), and then you use the header function
again to send the http header, this error will be returned, which is wrong It means: The HTTP header has been sent out, and you cannot modify it.
Why can using a buffer avoid this error?
Because the header function is not affected by the buffer, when it encounters the header function, PHP immediately executes apache to send this http header to the browser.
After PHP opens the output buffer for the output data, the data will be stored in the buffer, waiting for output. This can avoid the previous The error occurred.
2. When writing a file download program through PHP.
In order to make file downloading safer and improve more controllability, many friends like to use PHP to write file download pages. The principle It's very simple, just read and display the file content through fwrite, and then send the HTTP header through the header to let the browser know that this is an attachment, so that
can achieve the effect of providing download.
If you use the above The method of providing a download page will encounter an efficiency problem. If a file is large, say 100M, then without turning on the buffer output, all 100M of data must be read out, and then returned to the page at once. If In this way, the user will not get a response until all the data has been read
, which reduces the user experience.
If the output buffer is turned on, when the PHP program finishes reading a certain section of the file, it will immediately output it to apache, and then let apache return to the browser immediately, which can reduce the user's waiting time. What about the subsequent data? We can write a while loop to read the file section by section.
Every time a section is read, it will be read immediately Output until all files are output, so that the browser can continue to receive data without having to wait until all files have been read.
In addition, this approach also solves another very serious problem. For example A file is 100M. If the buffer is not turned on, all 100M files need to be read into the memory and then output. However, what if the PHP program has a memory limit? In order to ensure the stability of the server, administrators usually PHP execution
sets a memory limit (through the total memory_limit of php.ini, its default value is 8M), that is, the memory used by each PHP program cannot use more than this value. Assume that the value is 8M, and The file to be read is 100M, and there is simply not enough memory to read the file. At this time, we need to use the above
method to solve this problem, and only read a certain paragraph at a time, so that we can avoid Overcoming the memory limit
3. Static file caching
Many companies now have such a demand, that is, when a certain page is accessed for the first time, PHP will be executed, and then the displayed content will be returned to the browser. At the same time, The content displayed this time needs to be saved to the server, so that the next time you visit, the files saved on the server will be displayed directly without the need to operate through PHP.
This is the so-called "static page cache" ". So how can we return the content to the browser and save the data to the server at the same time? This requires the use of the output buffer.
ob_start(); echo 'aaa'; $string = ob_get_contents(); file_put_contents('a.html', $string); ob_flush(); flush();
Configuration related to the output buffer
In PHP.INI, there are two configuration items closely related to the buffer
1.output_buffering
This configuration directly affects the buffer of PHP itself, and there are three configuration parameters.on/off/xK (x is an integer value);
on - Turn on the buffer
off - Turn off the buffer
256k - Turn on the buffer, and when the content of the buffer exceeds 256k, the buffer will be automatically refreshed (Send data to apache);
2.implicit_flush
该配置直接影响apache的缓冲区,有2种配置参数. on/off
on - 自动刷新apache缓冲区,也就是,当php发送数据到apache的缓冲区的时候,不需要等待其他指令,直接就把输出返回到浏览器
off - 不自动刷新apache缓冲区,接受到数据后,等待刷新指令
与缓冲区有关的函数
1.ob_implicit_flush
作用和implicit_flush一样,是否自动刷新apache的缓冲区
2.flush
作用是发送指令到apache,让apache刷新自身的输出缓冲区.
3.ob_start
打开输出缓冲区,无论php.ini的文件如何配置,如果使用该函数,即使output_buffering设置成off,也会打开输出缓冲区
ob_start函数还接受一个参数,该参数是一个函数的回调,意思是,在输入缓冲区内容之前,需要使用调用传递进来的参数把缓冲区的内容处理一次,再放入缓冲区内
4.ob_flush
指示php本身刷新自身的缓冲区,把数据发送到apache
5.ob_clean
清除php缓冲区里面的内容
6.ob_end_clean
清除php缓冲区内的内容,并且关闭输出缓冲区
7.ob_end_flush
把php自身的缓冲区里的内容发送到apache,并把清除自身缓冲区内的内容
8.ob_get_clean
获取缓冲区的内容之后,清除缓冲区.
9.ob_get_contents
获取输出缓冲区里的内容
10.ob_get_flush
获取缓冲区里的内容,并且把这些内容发送到apache
11.ob_get_length
获取缓冲区里内容的长度
12.ob_list_handlers
获取运行ob_start时,所回调的函数名称, 例如:
ob_start(‘ob_gzhandler’);
print_r(ob_list_handlers);
将打印出ob_gzhandler;
13.ob_gzhandler
该函数的作用是作为ob_start的回调参数, 在缓冲区刷新之前,会调用该函数对数据进行到底gzip或者deflate压缩.这个函数需要zlib扩展的支持.
使用缓冲区的相关内容
1.ob_flush和flush的次序关系.上面的分析可以看出,ob_flush是和php自身相关的,而flush操作的是apache的缓冲区,所有我们在使用这两个函数的时候,需要先执行ob_flush,
再执行flush,因为我们需要先把数据从PHP上发送到apache,然后再由apache返回到浏览器.如果php还没有把数据刷新到apache,就调用了flush,则apache无任何数据返回到浏览器.
2.有的浏览器,如果接受到的字符太少,则不会把数据显示出来,例如老版的IE(必须要大于256k才显示).这样就会造成一个疑问, 明明在php和apache都进行了刷新缓冲区的操作,但是浏览器就是没有出现自己想要的数据,也许就是这个原因造成的.所以才测试的时候,可以在输出数据的后面加上多个空格,以填满数据,确定不会浏览器造成这类诡异的问题.
3.有些webserver,他自身的输出缓冲区会有一些限制,比如nginx,他有一个配置fastcgi_buffer_size 4k, 就是是表明,当自身的输出缓冲区的内容达到4K才会刷新,所以为了保证内容的数据,可以添加以下代码,保证内容长度
<?php echo str_repeat(" ",4096); ?>
4.在apache中,如果你开启了mod_gzip的压缩模块,这样可能会导致你的flush函数刷新不成功,其原因是,mod_gzip有自己的输出缓冲区,当php执行了flush函数,指示apache刷新输出缓冲区,但是内容需要压缩,apache就把内容输出到自身的mod_gzip模块,mod_gzip也有自身的输出 缓冲区,他也不会马上输出,所以造成了内容不能马上输出.为了改善这个情况,可以关闭mod_gzip模块,或者在httpd.conf增加以下内容,以禁止压缩
SetEnv no-gzip dont-vary
The above is the detailed content of what is php buffer?. For more information, please follow other related articles on the PHP Chinese website!