Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of PHP output cache function ob_start, flush, ob_flush

Detailed explanation of the usage of PHP output cache function ob_start, flush, ob_flush

伊谢尔伦
伊谢尔伦Original
2017-06-24 13:22:481485browse

Note that you need to restart the apache service line after modifying php.ini!

for($i=0;$i<10;$i++) {
  echo $i.&#39;<br />&#39;;
  flush();
  sleep(1);
}

Friends who know the PHP cache output control function must be familiar with the above code. The effect it wants to achieve is to output 1 number every 1 second, and it takes 10 seconds to complete the entire output. , but you will find strange phenomena in actual execution. Some people or sometimes it performs as you expect, while some people or sometimes it outputs 10 numbers at once after 10 seconds. I used to be crazy about this. A friend left a message saying that this situation is often because IE's cache must reach 256 characters before outputting. But in fact, I have also considered IE's situation before, but it still works sometimes and sometimes not. Condition. After reading the manual carefully today, I realized that there are reasons for these unpredictable phenomena.

It turns out that there are two key parameters in php.ini that will affect php's cache output control:

Parameter 1: output_buffering: on/off or integer. When set to on, the output cache control will be used in all scripts without limiting the size of the cache. When set to an integer, such as output_buffering=4096, when cache data reaches 4096 bytes, the cache will be automatically output and refreshed. The difference in this parameter is the reason why the above code has different execution results at different times. When output_buffering is turned off, all output (echo) of the script will be sent to the client immediately. When the above code is executed, a number will be output every second. After output_buffering is turned on, the output content will be cached on the server first, and will not be sent to the client until the end of the script.

Parameter 2: implicit_flush: on/off. Setting ON means that when the script has output, it is automatically sent to the client immediately. It is equivalent to automatically adding flush() after echo.

Related functions of php cache output control:

ob_start()

First parameter: Callback Function , optional. The output can be filtered or otherwise processed before it is cached. The most common usage is ob_start('ob_gzhandler'), which gzip-compresses the cached data before sending it to the client.

The second parameter: the size of the cache block, optional. If the cached content reaches or manipulates the cache block size, the cache will be automatically ejected. The default value is 0, which means that the size is not limited and the cache is cached until the end. There is also a special value 1, which represents chunk_size=4096.

The third parameter: whether to erase the cache, optional, the default is true, if set to false, the cache will not be cleared before the script execution ends.

You can use ob_get_contents() to obtain the data cached by the server in the form of a string, and use ob_end_flush() to cause the output to be cached data and turn off caching.

Using ob_end_clean() will silently clear the data cached on the server without any data or other actions.

The caches on the server are stacked, which means that after you enable ob_start() and before closing it, you can open another cache ob_start() inside it. However, you must also ensure that there are as many operations to turn off the cache as there are operations to turn the cache on.

ob_start() can specify a callback function to process cached data. If one ob_start() is nested inside another

ob_start(), we assume that the outer The ob_start() number of the layer is A, and the ob_start() number of the inner layer is B. They each have a callback function called functionA and functionB. Then when the data in cache B is output, it will be processed by the funcitonB callback function. , and then handed over to the outer functionA callback function for processing, and then can be output to the client.

In addition, the manual says that for some web servers, such as apache, using the callback function may change the current working directory of the program. The solution is to manually change the program in the callback function. To modify the working directory, use the chdir function. This does not seem to be encountered often. Remember to check the manual when you encounter it.

flush() and ob_flush()

The use of these two functions is probably the most confusing issue for many people. The explanation of the two functions in the manual is also unclear. The language is unclear and the difference between them is not clearly pointed out. It seems that the function of both is to refresh the output cache. But in the code at the beginning of our article, if flush() is replaced with ob_flush(), the program will no longer execute correctly. Obviously, there is a difference between them. Otherwise, it would be enough to directly state in the manual that one of them is an alias of another function. There is no need to explain them separately. So what is the difference between them?

反复研究了手册的说明,参考了手册中一些人的留言,自己琢磨应该是这样的:

在没有开启缓存时,脚本输出的内容都在服务器端处于等待输出的状态,flush()可以将等待输出的内容立即发送到客户端。

开启缓存后,脚本输出的内容存入了输出缓存中,这时没有处于等待输出状态的内容,你直接使用flush()不会向客户端发出任何内容。而ob_flush()的作用就是将本来存在输出缓存中的内容取出来,设置为等待输出状态,但不会直接发送到客户端,这时你就需要先使用ob_flush()再使用flush(),客户端才能立即获得脚本的输出。 

也就是说本文开头的脚本,可以根据缓存开启与否,有如下几种不同的写法: 

注:以下代码都未考虑IE缓存必须大于256字节才输出的问题,如在IE下测试,请在代码开始加一句:“echo str_repeat('',256)” 

写法1:

output_buffering = off
implicit_flush=off
 
for($i=0;$i<10;$i++) {
  echo $i.&#39;<br />&#39;;
  flush();
  sleep(1);
}

写法2:

output_buffering = on
implicit_flush=off
 
for($i=0;$i<10;$i++) {
  echo $i.&#39;<br />&#39;;
  ob_flush();
  flush();
  sleep(1);
}

写法3:

output_buffering = off
implicit_flush=off
 
ob_start();
for($i=0;$i<10;$i++) {
  echo $i.&#39;<br />&#39;;
  ob_flush();
  flush();
  sleep(1);
}

 写法4:

output_buffering = on
implicit_flush=off
 
ob_end_flush();
for($i=0;$i<10;$i++) {
  echo $i.&#39;<br />&#39;;
  flush();
  sleep(1);
}

 写法5:

output_buffering = on
implicit_flush=off
 
ob_end_clean();
for($i=0;$i<10;$i++) {
  echo $i.&#39;<br />&#39;;
  flush();
  sleep(1);
}

写法6:

output_buffering = on;
implicit_flush=on
 
ob_end_clean();
// 或者ob_end_flush();
for($i=0;$i<10;$i++) {
  echo $i.&#39;<br />&#39;;
  sleep(1);
}

 写法7:

output_buffering = on;
implicit_flush=on
 
ob_end_clean();
// 或者ob_end_flush();
for($i=0;$i<10;$i++) {
  echo $i.&#39;<br />&#39;;
  flush();
  sleep(1);
}

写法8:

output_buffering = off
implicit_flush=on
 
for($i=0;$i<10;$i++) {
  echo $i.&#39;<br />&#39;;
  sleep(1);
}

 

The above is the detailed content of Detailed explanation of the usage of PHP output cache function ob_start, flush, ob_flush. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn