PHP output cache function Ob_start,flush,ob_flush Usage

Source: Internet
Author: User
Note You need to restart the Apache service line after modifying PHP.ini!

for ($i =0; $i <10; $i + +) {  echo $i. ' <br/> ';  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.

The original php.ini has two key parameters that affect the PHP cache output control:

Parameter 1:output_buffering:on/off or integer. When set to on, output cache control is used in all scripts and does not limit the size of the cache. When set to an integer, such as output_buffering=4096, a flush cache is automatically output when the cached data reaches 4096 bytes. The difference between this parameter is what causes the code to perform differently at different times. When Output_buffering is off, all the output (echo) of the script is instantly sent to the client, and the code that executes it is the output of a number per second. When Output_buffering is turned on, the output is deferred to the server until the end of the script is sent to the client.

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

The relevant functions of the PHP cache output control:

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 is 0, which means the size is not qualified until the end of the cache. There is also a special value of 1, which represents chunk_size=4096.

The third parameter: whether to erase the cache, optional, the default is true, and if set to False, the cache will not be 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 nested inside 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 is Functiona and functionb, then in cache b in the data output, It will be the predecessor FUNCITONB callback function processing, and then to the outer Functiona callback function processing, before output to the client.

In addition, the manual said that for some Web servers, such as Apache, in the use of callback functions may change the current working directory of the program, the solution is to manually modify the working directory in the callback function back, with the ChDir function, this seems not often encountered, when encountered remember to check the manual it.

Flush () and Ob_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 Fush () is replaced with Ob_flush (), the program will no longer execute 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 instructions, referring to some of the manuals in the message, self-pondering should be this:

When the cache is not turned on, the contents of the script output are in the state of waiting for output on the server side, and flush () can send the content waiting for output 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, and you directly use flush () without sending anything to the client. The function of Ob_flush () is to remove the contents of the output cache, set to wait for the output state, but not directly to the client, you need to use Ob_flush () and then flush () before 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 IE cache must be larger than 256 bytes to output the problem, such as testing under IE, please add a sentence in the code: "Echo str_repeat (", " ,")

Notation 1:

Output_buffering = Offimplicit_flush=off for ($i =0; $i <10; $i + +) {  echo $i. ' <br/> ';  Flush ();  Sleep (1);}

Notation 2:

Output_buffering = Onimplicit_flush=off for ($i =0; $i <10; $i + +) {  echo $i. ' <br/> ';  Ob_flush ();  Flush ();  Sleep (1);}

Notation 3:

output_buffering = Offimplicit_flush=off Ob_start (); for ($i =0; $i <10; $i + +) {  echo $i. ' <br/> ';  Ob_flush ();  Flush ();  Sleep (1);}

notation 4:

output_buffering = Onimplicit_flush=off Ob_end_flush (); for ($i =0; $i <10; $i + +) {  echo $i. ' <br/> ';  Flush ();  Sleep (1);}

notation 5:

output_buffering = Onimplicit_flush=off Ob_end_clean (); for ($i =0; $i <10; $i + +) {  echo $i. ' <br/> ';  Flush ();  Sleep (1);}

Notation 6:

output_buffering = On;implicit_flush=on Ob_end_clean ()///Ob_end_flush (); for ($i =0; $i <10; $i + +) {  echo $i. ' <br/> ';  Sleep (1);}

notation 7:

output_buffering = On;implicit_flush=on Ob_end_clean ()///Ob_end_flush (); for ($i =0; $i <10; $i + +) {  echo $i. ' <br/> ';  Flush ();  Sleep (1);}

Notation 8:

output_buffering = Offimplicit_flush=on for ($i =0; $i <10; $i + +) {echo $i. ' <  br/> '; Sleep (1);} 

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.