The charm of PHP output cache

Source: Internet
Author: User
The charm of PHP output cache


One: Output Cache Introduction

In PHP's so-called output cache, the Echo or other output commands in the code are written to PHP buffer at the time of execution, and after the script executes or enforces the output cache command, the data is output to the browser (where PHP buffer Is the output_buffering set in PHP.ini, the default is on, which indicates unlimited size, can be changed to a number to limit the size).

Example:

Echo ' hlmblog.com ';
echo ' technology ';
echo ' Share ';


These two echoes are inserted sequentially into the buffer, and only the script execution completes or the cache output is forced to output the data to the browser.
If I want to output echo data in real time, see the following code:


I want to output echo data in real time, see the following code:

Ob_end_flush (); Close the PHP cache, or execute Ob_flush () before flush (), explained below
echo Str_pad ("", 256);
for ($i =5; $i >0; $i-) {
echo $i. ' <br> ';
Flush ();
Sleep (1);
}

Attention:
Attention:

1:flush and Ob_flush differences:
At first glance, the two are very similar, and many manuals are not explained clearly, the two are ambiguous, in fact, there is a very big difference.
When PHP.ini does not open the PHP buffer cache, the contents of the PHP script output will be in the server waiting for output state, will not be saved to the output cache, because the cache is not open, Flush can now be used to output content that waits for output to the client (browser or other output).
When PHP.ini opens the PHP buffer cache, the first step of the PHP script output is stored in the output cache, when the output content is no data, with flush words are no effect, no data. So first use Ob_flush to take out the content in the output cache to wait for the output state, and then use the flush to send the content to the client. The order of execution is first Ob_flush and then flush.
Therefore, to achieve real-time output, either using Ob_end_flush first switch off the PHP output cache directly flush, or first ob_flush and then flush.
2: The browser cannot output real-time data
Change the code to the following code, in Chrome Firefox IE and other browsers are disposable output, very wonderful phenomenon:

Ob_end_flush (); Close the PHP cache, or Ob_flush before flush ();
echo Str_pad ("", 256);
for ($i =5; $i >0; $i-) {
echo $i;
Flush ();
Sleep (1);
}

Find a half-day bug, and finally found a phenomenon, as long as the way to add an HTML tag, you can output real-time.
The reason is: only in the face of HTML tags will be instantaneous output, it is amazing, fortunately, the general output of the content will be with HTML tags, very few plain text.
Workaround: Add a carriage return or other HTML tags to solve the problem.
Two: Control the cache output can be used to do what, the specific few examples
1: Generate a static page
Static page loading speed is fast, this sentence is household known the truth, do not request database, this is how cool things ah.
Here's an example of generating a static page:

Echo Str_pad (', 1024);//Buffer overflow
Ob_start ();//Open buffer
$content = Ob_get_contents ();//Get the contents of the page output
$f = fopen ('./index.html ', ' W ');
Fwrite ($f, $content);//content written to TXT file
Fclose ($f);
Ob_end_clean ();//emptying and closing buffers

The legendary static pages are created in such a simple way.
2: Capture output

function test ($param) {
if ($param) {
Ob_start ();
eval ($param);
$contents = Ob_get_contents ();
Ob_end_clean ();
}else {
Echo ' regrets 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.