Home > Article > Backend Development > How to generate static pages with PHP
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
39a258508cc7aa99f6f387fca9ce064293f0f5c25f18dab9d176bd4f6de5d30eb2386ffb911b14667cb8f0f91ea547a7PHP website static tutorial6e916e0f7d1e588d4f442bf645aedb2f9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86dWelcome to the PHP website development tutorial website www.leapsoul.cn. This article mainly introduces the staticization of PHP website pages. Method36cc49f0c466276486e50c850b7e495673a6ac4ed44ffec12cee46588e518a5e"; $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"; } ?>
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!