PHP dynamic to static principle_PHP tutorial

WBOY
Release: 2016-07-21 15:59:43
Original
839 people have browsed it

As far as I know, there are two ways to use PHP to output static pages. One is to use template technology, and the other is to use ob series functions. Both methods look similar, but in fact they are different.

The first way: using templates . There are currently many 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

Copy the code The code is as follows:
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 content. Now in 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 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 non-file header information from the PHP program will not be sent. Instead, it is 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 equivalent to the fetch() above. The reason Same. Code:
Copy code The code is as follows:
ob_start();
echo "Hello World! ";
$content = ob_get_contents();//Get all the content output by the php page
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317259.htmlTechArticleAs far as I know, there are two ways to use PHP to output static pages. One is to use template technology , the other is to use the ob series function. Both methods look similar, but in fact...
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!