How to use ob_start to buffer output for simple caching _ PHP Tutorial

Source: Internet
Author: User
Use ob_start to buffer the output for simple caching. The PHPob_start () function is a powerful function that can help us solve many problems. The OutputControl function allows you to control the output of data in the script. The PHP ob_start () function is a powerful function that can help us solve many problems.

The Output Control function allows you to freely Control the Output of data in the script. It is very useful, especially when you want to output the file header after the data has been output. The output control function does not affect the header information sent using header () or setcookie (). it only applies to data blocks similar to echo () and PHP code.

Anyone who knows about the header () function knows that this function will send a file header to the browser, but if any output (including null output, for example, a blank space, carriage return, or line feed will prompt an error. If we remove ob_start () from the first line and execute this program, we will find an error message: "Header had all ready send "! However, when ob_start is added, no error will be prompted because when the buffer zone is opened, the characters after echo will not be output to the browser, but will be retained on the server until you use flush or ob_end_flush, so there will be no file header output errors!

The following describes how to use ob_start for simple caching.

 ';} Echo"
"; $ Time2 = microtime (true); echo $ time2-$ time1; // output 0.0010678768158?>

When no cache is performed, the runtime is 0.0010678768158.

1. simple cache
 "; $ Info = ob_get_contents (); file_put_contents ($ cache_file, $ info); $ time2 = microtime (true); echo $ time2-$ time1; // 0.00075888633728 output?>

It takes 0.001 seconds to complete the cache, and 0.0007 seconds to complete the simple cache. the cache speed is slightly improved.

2. further cache

On the basis of the previous cache, a line is deepened. As we all know, js files will not only consume server resources, but will also be downloaded to the client and downloaded once in order, without consuming bandwidth. The disadvantage is that they cannot be captured by search engines, however, it is a good choice for office systems.

 "; $ Info = ob_get_contents (); $ info = htmltojs ($ info); file_put_contents ($ cache_file, $ info); $ time2 = microtime (true ); echo $ time2-$ time1;?>

It is just a simple way to provide a cache.

The http://www.bkjia.com/PHPjc/752543.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752543.htmlTechArticlePHP ob_start () function is a powerful function that can help us deal with many problems. The Output Control function allows you to freely Control the Output of data in the script. It is very useful...

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.