PHP uses ob_start to generate html pages

Source: Internet
Author: User

PHP uses ob_start to generate html pages

Ob_start ([string output_callback])-open the output buffer

All output information is not directly sent to the browser, but saved in the output buffer. An optional callback function is used to process output result information.

Ob_end_flush-end (send) output buffer content, disable the output buffer

The output control function allows you to freely control the output of data in the script, which is useful when we want to output data before the header.

The Code is as follows:

<? Php
Ob_start (); // open the buffer
Echo "output n"; // output
Header ("header information ");
Ob_end_flush (); // output all content to the browser
?>



Most people use ob when generating static html, when a page is not refreshed, when other users browse this page again, the program will no longer call php and related database tutorials. In this case, using ob to generate html is a good practice.

The Code is as follows:

<? Php
Ob_start ();
If (@ readfile ($ tem_path) {// write the content in the specified path to the cache. If not, false is returned (that is, a PHP file you want to convert to html)
$ Content = ob_get_contents (); // obtain the cached content.
$ Fp = fopen ("1.html", "w"); // create a file, open it, and write
Fwrite ($ fp, $ content); // write the content of the PHP page to 1.html
}
Fclose ($ fp );
Ob_clean ();
?>

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.