PHP dynamic to static principle _ PHP Tutorial

Source: Internet
Author: User
The principle of dynamic and static PHP changes. I know the method of outputting static pages using PHP. one is to use the template technology, and the other is to use the ob series functions. The two methods look similar, but in fact I know the method of outputting static pages using PHP. There are two methods, one is using the template technology, the other is to use ob series functions. Both methods look similar, but they are actually different.

First: use templates. At present, PHP templates can be said to be many, including powerful smarty and easy-to-use smarttemplate. Each of these templates has a function to get the output content. We use this function to generate static pages. The advantage of using this method is that the code is clear and readable.

Here I use smarty as an example to illustrate how to generate static pages.

The code is as follows:

Require ('smarty/smarty. class. php ');
$ T = new Smarty;
$ T-> assign ("title", "Hello World! ");
$ Content = $ t-> fetch ("templates/index.htm ");
// Here fetch () is the function used to obtain the Output content. in the $ content variable, it is the content to be displayed.
$ Fp = fopen ("archives/2005/05/19/0001 .html", "w ");
Fwrite ($ fp, $ content );
Fclose ($ fp );
?>


Method 2: Use ob functions. The functions used here are mainly ob_start (), ob_end_flush (), ob_get_content (), where ob_start () indicates to open the browser buffer. after the buffer is enabled, all non-file header information from PHP programs is not sent, but stored in the internal buffer until you use ob_end_flush (). the most important function here is ob_get_contents (). The function is used to obtain the buffer content, which is equivalent to the fetch () above. Code:

The code is as follows:

Ob_start ();
Echo "Hello World! ";
$ Content = ob_get_contents (); // retrieves all the content output on the php page.
$ Fp = fopen ("archives/2005/05/19/0001 .html", "w ");
Fwrite ($ fp, $ content );
Fclose ($ fp );
?>

Bytes. Both methods look similar, but actually...

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.