Charm of php output cache

Source: Internet
Author: User
Charm of php output cache 1: Introduction to the output cache in php: echo or other output commands in the code are first written to phpbuffer during execution, after the script is executed... charm of php output cache


I. INTRODUCTION to output cache

In php, the so-called output cache means that the echo or other output commands in the code are first written to the php buffer during execution. after the script is executed or the output cache command is forcibly executed, the data will be output to the browser (where php buffer is php. output_buffering set in ini. the default value is on, indicating unlimited size. you can change it to a number to limit the size ).

Example:

Echo 'hlmblog. com ';
Echo 'techno ';
Echo 'share ';


These two echo values are inserted to the buffer in sequence. data is output to the browser only after the script execution is complete or the cache output is enforced.
If I want to output echo data in real time, see the following code:


To output the echo data in real time, see the following code:

Ob_end_flush (); // disable the php Cache, or execute ob_flush () before flush (). The following explains
Echo str_pad ("", 256 );
For ($ I = 5; $ I> 0; $ I --){
Echo $ I .'
';
Flush ();
Sleep (1 );
}

Note:
Note:

1: Difference between flush and ob_flush:
At first glance, the two are similar, and many manuals are unclear. In fact, the two are quite different.
When php. when ini does not enable the php buffer cache, the content output by the php script will be in the waiting output status on the server and will not be saved to the output cache because the cache is not enabled, in this case, flush can be used to immediately output the content waiting for output to the client (browser or other output terminals ).
When php. after ini enables the php buffer cache, the first step of the php script output content is stored in the output cache. at this time, when the output content is no data, it will be ineffective if flush is used, data is not retrieved. Therefore, you must first use ob_flush to retrieve the content in the output cache and change it to the waiting state. Then, use flush to send the content to the client. The execution sequence is ob_flush and then flush.
Therefore, to achieve real-time output, either use ob_end_flush to disable the php output cache and then directly flush, or use ob_flush to flush.
2: The browser cannot output real-time data.
The code is changed to the following code, which is output at one time in browsers such as chrome firefox ie, which is a wonderful phenomenon:

Ob_end_flush (); // disable php Cache, or ob_flush () before flush ();
Echo str_pad ("", 256 );
For ($ I = 5; $ I> 0; $ I --){
Echo $ I;
Flush ();
Sleep (1 );
}

After looking for a bug for half a day, I finally found a problem. I just need to add an html tag to output it in real time.
The reason is: it is amazing that html tags are output immediately only when html tags are encountered. Fortunately, html tags are usually included in the output content and few plain texts are used.
Solution: Add a carriage return or other html tags to solve the problem.
II. control what the cache output can be used for. Examples
1: generate static pages
The loading speed of static pages is fast. This sentence is just a matter of saying that you do not need to request a database.
The following is an example of generating a static page:

Echo str_pad ('', 1024); // buffer overflow
Ob_start (); // open the buffer
$ Content = ob_get_contents (); // get the page output content
$ F = fopen ('./index.html', 'w ');
Fwrite ($ f, $ content); // write the content to the txt file
Fclose ($ f );
Ob_end_clean (); // clears and closes the buffer.

The legendary static page is simply generated.
2: capture output

Function test ($ param ){
If ($ param ){
Ob_start ();
Eval ($ param );
$ Contents = ob_get_contents ();
Ob_end_clean ();
} Else {
Echo 'Sorry, no output ';
Exit ();
}
Return $ contents;
}

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.