PHP Output Cache Usage Example Tutorial

Source: Internet
Author: User
This article describes the use of the PHP output cache, in PHP's so-called output cache, that is, the echo or other output commands in the code are written to PHP buffer at execution time, after the script executes or enforces the output cache command, the data will be output to the browser.

One, what is the PHP output cache in PHP so-called output cache, that is, the code of ECHO or other output commands are written to PHP buffer at execution time, after the script executes or enforces the output cache command, the data will be output to the browser (where PHP Buffer is the output_buffering set in php.ini, default is on, indicating unlimited size, can be replaced by numbers 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. Output echo data in real time:

Ob_end_flush (); Close the PHP cache, or execute Ob_flush () before Flush (), with the following explanation for Echo Str_pad ("", 256); for ($i =5; $i >0; $i-) {echo $i. '
'; Flush (); Sleep (1); }

Attention:

1:flush and Ob_flush differences: At first glance the two are very similar, and many of the manuals are not clear explanation, the two are ambiguous, in fact, there are very big differences. 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 can not output the real-time data modified code, in Chrome Firefox IE and other browsers are one-off output:

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);}

Just add an HTML tag and you can output it in 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.

Second, the control cache output Example 1, the static page static page loading speed is fast

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

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; }
  • Related Article

    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.