Principle of dynamic and static PHP changes

Source: Internet
Author: User

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. 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.
Copy codeThe Code is as follows: <? Php
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:
Copy codeThe Code is as follows: <? Php
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 );

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: 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.