How to generate static pages with PHP
The first one: using templates.
There are currently a lot of PHP templates, including the powerful smarty and the simple and easy-to-use smarttemplate. Each of their templates has a function to get the output content. The way we generate static pages is to use this function. The advantage of using this method is that the code is clearer and more readable.
Here I use smarty as an example to illustrate how to generate a static page:
<?php require("smarty/Smarty.class.php"); $t = new Smarty; $t->assign("title","Hello World!"); $content = $t->fetch("templates/index.htm"); //这里的 fetch() 就是获取输出内容的函数,现在$content变量里面,就是要显示的内容了 $fp = fopen("archives/2005/05/19/0001.html", "w"); fwrite($fp, $content); fclose($fp); ?>
The second method: use the ob series of functions.
The functions used here are mainly ob_start(), ob_end_flush(), ob_get_content(), where ob_start() means to open the browser buffer. After opening the buffer, all data from the PHP program No non-file header information will be sent, but stored in the internal buffer until you use ob_end_flush(). The most important function here is ob_get_contents(). The function of this function is to obtain the contents of the buffer, which is quite The same principle applies to the fetch() above.
<?php ob_start(); echo "Hello World!"; $content = ob_get_contents();//取得php页面输出的全部内容 $fp = fopen("archives/2005/05/19/0001.html", "w"); fwrite($fp, $content); fclose($fp); ?>
For more PHP knowledge, please visit the PHP Chinese website 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!