How to generate static pages with PHP

WJ
Release: 2023-04-08 17:52:01
forward
4567 people have browsed it

What is PHP staticization

#The simple understanding of PHP staticization is to make the website-generated pages displayed in front of visitors in the form of static HTML. PHP staticization It is divided into pure static and pseudo-static. The difference between the two lies in the processing mechanism of PHP generating static pages.

How to generate static HTML pages with PHP

1. Use PHP templates to generate static pages

PHP templates are very convenient to achieve staticization, such as installation and Use PHP Smarty to achieve static website.

2. Use PHP file reading and writing functions to generate static pages

PHP generates static page example code

PHP website static tutorial Welcome to the PHP website development tutorial website www.leapsoul.cn. This article mainly introduces the staticization of PHP website pages. Method"; $fp = fopen("leapsoulcn.html","w"); if(!$fp) { echo "System Error"; exit(); } else { fwrite($fp,$out1); fclose($fp); echo "Success"; } ?>

3. Use PHP output control function (Output Control) to generate a static page

The output control function (Output Control) uses and controls the cache to generate static HTML pages, and also uses PHP file reading and writing functions.

PHP generates static page example code

<? ob_start();
echo "<html>" . "<head>" . "<title>PHP网站静态化教程</title>" . "</head>" . "<body>欢迎访问PHP中文网www.php.cn,本文主要介绍PHP网站页面静态化的方法</body>" . "</html>";
$out1 = ob_get_contents();
ob_end_clean();
$fp = fopen("leapsoulcn.html", "w");
if (!$fp) {
    echo "System Error";
    exit();
} else {
    fwrite($fp, $out1);
    fclose($fp);
    echo "Success";
} ?>
Copy after login

We know that using PHP for website development, the execution results are generally output directly to the browser. In order to use PHP to generate static pages, you need to use the output The control function controls the cache area to obtain the content of the cache area, and then outputs it to the static HTML page file to achieve static website.

The idea of ​​PHP generating a static page is: first turn on the cache, and then output the HTML content (you can also include the HTML content in the form of a file through include), then obtain the content in the cache, clear the cache and pass PHP file reading and writing functions write cached content into static HTML page files. Tutorial on reading and writing PHP files?

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

Knowledge points:

1. The ob_start function is generally used to enable caching. Note that there cannot be any output before using ob_start, such as spaces, characters, etc.

2. The ob_get_contents function is mainly used to obtain the content in the cache and return it in the form of a string. Note that this function must be called before the ob_end_clean function, otherwise the cache content cannot be obtained.

3. The ob_end_clean function mainly clears the contents of the cache and closes the cache. It returns True if successful and False if failed.

The PHP output control function (Output Control) has many applications and will be used in the future. unfold one after another.

So far, the method of using PHP to generate static HTML pages to achieve website staticization has been introduced. You can choose different staticization methods according to the actual situation and needs.

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of How to generate static pages with PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template