Home > Article > Backend Development > What is the function of buffer in php
What is a buffer?
Simply put, the function of the buffer is to put the input or output content into the memory first without displaying or reading it. 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 PHP's buffer, 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 to output the contents of the buffer, it will output the data in the buffer to apache. Apache receives the data output by PHP and then outputs the data. The data is stored in apache's own buffer and waits until output.
When apache receives the instruction and only wants to output the contents of the buffer, it will output the contents of the buffer and return it to the browser.
Steps: Execute php---->(encounter echo, print_r and other output), put the output data into the buffer of php itself, wait for output---->(receive the output buffer Area content) data is output to apache and stored in apache's own buffer----> (receive output specification) buffer content is output and returned to the browser
echo, print => ; php output_buffering => webServer buffer => browser buff => browser display
That is: script output => php buffer setting => system buffer setting (apache, nginx) => Browser buffer settings=> Displayed to the user
It can be seen that when PHP wants to output data, it will go through two buffers (first its own, and then is apache), and then return to the browser.
Related recommendations: "PHP Introduction Tutorial"
What role does the buffer play in PHP?
1. The most common one is that some data has been output before using the header function, which will cause certain errors, such as Cannot modify header information – headers already sent by;
echo "this is test"; header("LOCATION http://www.baidu.com");
The reason for this error is that some data has been output before the header, and while outputting this data, apache will simultaneously send a response status to the browser (since there is output, that is, this the request is valid), and then you use the header function again.
When sending the http header, this error will be returned. The error means: the HTTP header has been sent 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 the output data PHP opens the output buffer, the data will be stored in the buffer, waiting for output. This way you can avoid the mistakes that happened before.
2. When writing a file download program through PHP. ((Example) The principle of PHP's method of implementing HTTP breakpoint resumable download)
In order to make file downloading safer and improve more controllability, many friends like to use PHP to write file download pages. The principle is very simple, which is to 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 the download effect can be achieved.
If you use the above method to provide a download page, you will encounter an efficiency problem. If a file is large, assuming it is 100M, then all 100M data must be read without turning on the buffer output. out, and then return to the page all at once. If you do this, the user will not get a response until all the data has been read, which reduces the user experience.
If the output buffer is enabled, 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, thus reducing the user's waiting time. What to do with the subsequent data? We can write a while loop to read the file piece by piece. Each time a piece is read, it will be output immediately until all the files are output. In this way, the browser can continue to receive data without having to wait until all files are read. In addition, this approach also solves another very serious problem.
For example, if a file is 100M, if the buffer is not enabled, the entire 100M file needs to be read into the memory and then output. But what if the PHP program has memory limits? In order to ensure the stability of the server, administrators usually set a limit on PHP's execution memory (through the total memory_limit in php.ini, the default value is 8M), that is, the memory used by each PHP program cannot exceed this value. . Assume that the value is 8M and the file to be read is 100M. There is simply not enough memory to read the file. At this time, we need to use the above method to solve this problem. Only read a certain paragraph at a time, so as to avoid memory limitations.
3.静态文件缓存
现在很多公司有这么一个需求,就是某一个页面在第一次访问的时候,会执行PHP,然后把显示的内容返回到浏览器,同时需要把这次显示的内容保存到服务器上,这样下次访问的时候,就直接把保存在服务器上的文件直接显示,而不需要通过PHP来做操作这就是所谓的”静态页面缓存”。那怎么样才能做到把内容返回到浏览器的同时把数据保存到服务器上呢?这就要用到输出缓冲区了。
ob_start(); echo 'aaa'; $string = ob_get_contents(); file_put_contents('a.html', $string); ob_flush(); flush();
与输出缓冲区有关的配置
在PHP.INI中,有两个跟缓冲区紧密相关的配置项
1.output_buffering
该配置直接影响的是php本身的缓冲区,有3种配置参数:on/off/xK(x为某个整型数值);
on - 开启缓冲区
off - 关闭缓冲区
256k - 开启缓冲区,而且当缓冲区的内容超过256k的时候,自动刷新缓冲区(把数据发送到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增加以下内容,以禁止压缩。
The above is the detailed content of What is the function of buffer in php. For more information, please follow other related articles on the PHP Chinese website!