105. Output Control Cache

Source: Internet
Author: User

<?phpfor ($i = 0; $i < 5; $i++) {    echo $i;    ob_flush(); //如何开启缓存的话,要加这句话    flush();    sleep(1);}

A friend of the PHP cache output control function must be familiar with the above code, it would like to achieve the effect is 1 seconds output 1 digits, complete the output takes 10 seconds, but in practice you will find strange phenomenon, some people or sometimes it behaves as you wish, Some people may sometimes be 10 seconds before outputting 10 digits at a time. I used to be crazy about this, a friend left to speak this situation is often because IE cache must reach 256 characters to output, but in fact, I have also considered the situation of IE, can still sometimes the spirit of the situation. Read the handbook carefully today to understand that these unforeseen phenomena have its reasons.

原来php.ini中有两个关键参数会影响到php的缓存输出控制:参数1:output_buffering :on/off 或者整数。设置为on时,将在所有脚本中使用输出缓存控制,不限制缓存的大小。而设置为整数时,如output_buffering=4096,当缓存数据达到4096字节时会自动输出刷新缓存。而这个参数的不同正是导致以上代码在不同时候执行结果不同的原因。当output_buffering关闭时,脚本所有的输出(echo)都会即时发送到客户端,执行上面代码时就是每秒输出一个数字。而开启output_buffering后,输出内容就会先缓存在服务端,直到脚本结束时才一起发送给客户端。
ob_start()

First parameter: callback function, optional. It can be filtered or otherwise processed before the output is cached. The most common usage is ob_start (' Ob_gzhandler '), which is to gzip compress the cached data before sending it to the client. Second parameter: The size of the cache block, optional. If the cached content reaches or operates the cache block size, the cache is automatically output. The default value is0, which means the size is not limited, and the cache is until the end. There's a special value.1, representing chunk_size=4096。 The third parameter: whether to erase the cache, optional, default istrue, if set tofalse, the cache is not purged until the script execution is complete. You can use Ob_get_contents () to obtain data from the server-side cache as a string, using Ob_end_flush () to output cached data and close the cache. Using Ob_end_clean () silently clears the data cached by the server, without any data or other behavior. The server-side cache is stacked, which means you can also open another cache Ob_start () within the Ob_start () after you turn it off. But you also have to make sure that the cache is turned off as much as the number of open cache operations. ob_start() can specify a callback function to handle the cached data if one ob_start() is internally nested within another Ob_start (), we assume that the outer ob_start(), the number is a, the inner layer of the Ob_start() number is B, they each developed a callback function respectively is Functiona and functionb, the data output in cache B will be processed by the predecessor FUNCITONB callback function, and then to the outer Functiona The callback function is processed before it can be output to the client. In addition, the manual says that for some web servers, such as Apache, the use of callback functions may change the current working directory of the program, the workaround is to manually modify the working directory in the callback function back, with chdir function, which seems to be infrequently encountered, remember to check the manual when you meet. 
Flush()AndOb_flush()The use of these two functions is even the most puzzling problem of many people, the manual on the interpretation of two functions are also vague, do not explicitly point out their differences, it seems that both of the function is to refresh the output cache. But in the code at the beginning of our article, if we sayFush()Replaced byOb_flush(), the program can no longer be executed correctly. Obviously, they are different, or else the manual directly indicates that one of the other functions is an alias, there is no need to explain separately. So what is the difference between them? Repeated study of the manual, referring to some of the manual of the message, I think it should be this: when the cache is not turned on, the content of the script output in the server side is waiting for output state,Flush()Content that waits for output can be sent to the client immediately. When the cache is turned on, the contents of the script output are stored in the output cache, and there is no content waiting for the output state, you directly useFlush()No content is emitted to the client. andOb_flush()is to remove the content from the output cache and set it to wait for the output state, but not send it directly to the client, you need to first use theOb_flush()Re-useFlush(), the client can immediately get the output of the script. In other words, the script at the beginning of this article can be opened according to the cache or not, there are several different ways: note: The following code does not consider the IE cache must be greater than 256 bytes to output the problem, such as under IE Test, please add a sentence in the code: "Echo str_repeat(",")”


也就是说,ob_flush()将 ob 缓存输入到程序缓存,flush()将程序缓存输出到浏览器缓存。

An explanation of the Output Control function


Http://www.admin10000.com/document/1100.html

105. Output Control Cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.