How to clear the buffer in php: 1. Use the "ob_clean()" function to clear the contents of the php buffer; 2. Use the "ob_end_clean()" function to clear the php buffer. The contents of the buffer and close the output buffer.
Recommended: "PHP Video Tutorial"
php method to clear the buffer
##Method 1: ob_end_clean
ob_end_clean - Clear (erase) the buffer and close the output bufferSyntax:ob_end_clean ( void ) : bool
Method 2: Use ob_clean()
ob_clean - clear (erase) the output bufferInstructionsob_clean ( void ) : void
Expand knowledge
What is a buffer?
In simple terms, The function of the 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 broad question. If you are interested, you can find information on the Internet. 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.).Where is it useful when PHP is executed? To the buffer?
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 When encountering 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 instructions, it is instructed to output When reading the contents of the buffer, the data in the buffer will be output to apache. Apache receives the data output by PHP, and then stores the data in apache's own buffer, and waits until the 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 Like output) Put the output data into PHP's own buffer and wait for output---->(Receive the contents of the output buffer) The data is output to apache and stored in apache's own buffer----> (Receive output specification) Output the buffer content and return it to the browserecho、print => php output_buffering => webServer buffer => browser buff => browser display
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. This will cause certain errors, such as Cannot modify header information – headers already sent by;echo "this is test"; header("LOCATION http://www.baidu.com");
如果用上面的办法提供下载页面,会碰到一个效率问题,如果一个文件很大,假设为100M,那么在不开启缓冲区输出的情况下,必须要把100M数据全部读出,然后一次返回到页面上,如果这样做,用户将会在所有数据读完之后才会得到响应,降低了用户体验感.
如果开启了输出缓冲区,当PHP程序读完文件的某一段,然后马上输出到apache,然后让apache马上返回到浏览器,这样就可以减少用户等待时间.那后面的数据怎么办呢?我们可以写一个while循环,一直一段一段地读取文件每读一段,就马上输出,直到把文件全部输出为止,这样浏览器就可以持续地接受到数据,而不必等到所有文件读取完毕. 另外,该做法还解决了另外一个很严重的问题.例如一个文件是100M,如果不开启缓冲区的情况下,则需要把100M文件全部读入内存,然后再输出.但是,如果PHP程序做了内存限制呢?为了保证服务器的稳定,管理员通常会把PHP的执行
内存设一个限制(通过php.ini总的memory_limit, 其默认值是8M), 也就是每个PHP程序使用的内存不能使用超过这个值的内存. 假设该值为8M,而要读入的文件是100M,根本就没有足够的内存来读入该文件.这个时候,我们就需要用到上面的
办法来解决这个问题,每次只读某一段,这样就可以避免了内存的限制
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扩展的支持.
The above is the detailed content of How to clear buffer in php?. For more information, please follow other related articles on the PHP Chinese website!