PHP static page generation method

Source: Internet
Author: User
In PHP website development, full-site or partial static processing is required for website promotion and SEO. PHP provides multiple methods to generate static HTML pages, for example, to use PHP templates and cache to achieve static pages, we will discuss how PHP generates static pages in the form of PHP instance tutorials today .... "> <LINKhref =" http://www.php100.com//statics/st

In PHP website development, full-site or partial static processing is required for website promotion and SEO. PHP provides multiple methods to generate static HTML pages, for example, to use PHP templates and cache to achieve static pages, we will discuss how PHP generates static pages in the form of PHP instance tutorials today. There are two static page methods: pseudo-static, url rewriting, and static. The following describes the static page method in PHP.

What is PHP static

A simple understanding of PHP static is to make website pages displayed in the form of static HTML in front of visitors. PHP static is divided into pure static and pseudo static, the difference between the two is that the processing mechanism for generating static pages in PHP is different.

PHP static HTML page generation method

1. use PHP templates to generate static pages

It is very convenient to implement static PHP templates, such as installing and using PHP Smarty to implement static websites.

2. use the php file read/write function to generate a static page

PHP generates static page instance code

3. use the Output Control function of PHP to generate a static page.

The Output Control function uses and controls the cache to generate static HTML pages. It also uses the php file read/write function.

PHP generates static page instance code

We know that PHP is used for website development. Generally, the execution results are directly output to the browser. to generate a static page using PHP, you need to use the output control function to control the cache to obtain the content in the cache, and then output to the static HTML page file to achieve static website.

The idea of generating static pages in PHP is: first enable caching, then output HTML content (you can also include HTML content in the form of a file through include), and then get the content in the cache, after the cache is cleared, the PHP file read/write function writes the cached content to the static HTML page file. Php file read/write tutorial?

The process of obtaining the output cache content to generate a static HTML page requires three functions: ob_start (), ob_get_contents (), and ob_end_clean ().

Knowledge Point:

1. the ob_start function is generally used to enable caching. Note that no output, such as spaces or characters, is allowed before the ob_start function is used.

2. the ob_get_contents function is mainly used to obtain the cached content and return it as a string. Note that this function must be called before the ob_end_clean function; otherwise, the cached content cannot be obtained.

3. the ob_end_clean function clears the content in the cache and closes the cache. if the cache is successful, True is returned. if the cache fails, False is returned.

The Output Control function of PHP has many applications and will be available in the future.

At this point, the method for generating static HTML pages using PHP to achieve static website is described. you can choose different static methods based on the actual situation and requirements.

  1. $ Out1 ="PHP static website tutorial
  2. Welcome to www.leapsoul.cn for PHP website development. This article mainly introduces the static PHP website page method.
  3. ";
  4. $ Fp = fopen ("leapsoulcn.html", "w ");
  5. If (! $ Fp)
  6. {
  7. Echo "System Error ";
  8. Exit ();
  9. }
  10. Else {
  11. Fwrite ($ fp, $ out1 );
  12. Fclose ($ fp );
  13. Echo "Success ";
  14. }
  15. ?>
    1. Ob_start ();
    2. Echo"".
    3. "".
    4. "PHP static website tutorial".
    5. "".
    6. "Welcome to www.leapsoul.cn for PHP website development. This article mainly introduces the static PHP website page method.".
    7. "";
    8.  
    9. $ Out1 = ob_get_contents ();
    10. Ob_end_clean ();
    11. $ Fp = fopen ("leapsoulcn.html", "w ");
    12. If (! $ Fp)
    13. {
    14. Echo "System Error ";
    15. Exit ();
    16. }
    17. Else
    18. {
    19. Fwrite ($ fp, $ out1 );
    20. Fclose ($ fp );
    21. Echo "Success ";
    22. }
    23. ?>

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.