The way to output static pages in PHP, as I know, has 2 kinds, one is using template technology, the other is using OB series functions. Both methods look the same, but in fact, they are different.
The first: the use of templates。 The current PHP template can be said to be a lot of, there is a powerful smarty, there are simple and easy-to-use smarttemplate and so on. Each of these templates has a function that gets the output. The way we generate static pages is to take advantage of this function. The advantage of this method is that the code is clearer and more readable.
Here I use Smarty as an example to show how to generate a static page
Copy Code code as follows:
<?php
Require (' smarty/smarty.class.php ');
$t = new Smarty;
$t->assign ("title", "Hello world!");
$content = $t->fetch ("templates/index.htm");
The fetch () here is the function to get the output, and now the $content variable is the content to be displayed.
$fp = fopen ("archives/2005/05/19/0001.html", "w");
Fwrite ($fp, $content);
Fclose ($FP);
?>
The second method: Use functions from the OB series. The functions used here are mainly Ob_start (), Ob_end_flush (), Ob_get_content (), where Ob_start () is to open the browser buffer, when the buffer is turned on, all non-file header information from the PHP program will not be sent, Instead, save it in the internal buffer until you use the Ob_end_flush (). And the most important function here is ob_get_contents (), the function of which is to get the contents of the buffer, which is equivalent to the fetch (). Code:
Copy Code code as follows:
<?php
Ob_start ();
echo "Hello world!";
$content = ob_get_contents ()//Get all content of PHP page output
$fp = fopen ("archives/2005/05/19/0001.html", "w");
Fwrite ($fp, $content);
Fclose ($FP);
?>