Let's look at some basic concepts first.
 
One, PHP script and dynamic page.
 
PHP script is a server-side scripting, can be embedded and other methods and HTML files mixed, but also class, function encapsulation, such as the form of templates to the user request processing. In any way, the rationale for it is this. The client requests a page-----> Web server to introduce the specified script for processing-----> script is loaded into the server-----> The PHP parser specified by the server parses the script to form an HTML language----> The parsed HTML statement is passed back to the browser as a package. It is not difficult to see that after the page is sent to the browser, PHP does not exist, has been transformed into HTML statements. Customer request for a dynamic file, in fact, there is no real file exists there, PHP is parsed into a corresponding page, and then sent back to the browser. This type of page processing is called a "dynamic page".
 
Two, static page.
 
A static page is a page that does exist on the server side with only HTML and client-run scripts such as JS,CSS. The way it's handled is. The client requests that a page----> Web server confirm and load a page----> the Web server passes the page back to the browser as a package. By this process, we compare the dynamic page, you can square now. Dynamic pages need to be resolved by the PHP parser of the Web server, and usually need to connect the database, do database access operation, then form the HTML language packet, and static page, no need to parse, no need to connect the database, send directly, can greatly reduce the server pressure, improve the server load capacity, Dramatically provide page opening speed and overall site opening speed. However, the disadvantage is that the request cannot be processed dynamically, and the file must exist on the server.
 
Third, template and template parsing.
 
The template is not yet populated with the content HTML file. For example:
 
Temp.html
 
 <HTML>
 <TITLE>{ title }</TITLE>
 <BODY>
  this is a { file } file's templets
 </BODY>
</HTML>PHP Processing:
 
templetest.php
 
 <?php
 $title = "拓迈国际测试模板";
 $file = "TwoMax Inter test templet,<br>author:Matrix@Two_Max";
 $fp      = fopen ("temp.html","r");
 $content = fread ($fp,filesize ("temp.html"));
 $content .= str_replace ("{ file }",$file,$content);
 $content .= str_replace ("{ title }",$title,$content);
 echo $content;
?>Template parsing processing, the PHP script will be resolved after processing the results of the fill (content) into the template processing process. Typically, the use of template classes. At present, the more popular template parsing class has phplib,smarty,fastsmarty and so on. The rationale for template parsing is usually replacement. Also some programmers are accustomed to the judgment, loop and other processing into the template file, with the Analytic class processing, the typical application of the block concept, simply for a circular processing. By the PHP script to specify the number of cycles, how to loop generation, and then by the template parsing class to implement these operations.
 
OK, compared to the static page and dynamic page of their pros and cons, now we say, how to use PHP to generate static files.
 
PHP generated static page does not refer to PHP dynamic parsing, output HTML page, but refers to the creation of HTML pages in PHP. Also because the HTML is not writable, we create HTML if there is a modification, you need to delete the rebuild can be. (Of course, you can also choose to modify it, but personally think that it is better to delete the regeneration faster, some outweigh the gains.) )
 
Anyway PHP fans who have used PHP file manipulation functions know that PHP has a file operation function fopen, that is, open the file. If the file does not exist, attempt to create it. This is the rationale that PHP can use to create HTML files. You can create a file as long as the folder that holds the HTML file has write permissions (that is, permission definition 0777). (For Unix systems, the win system is not considered.) Still, for example, if we modify the last sentence and specify that a static file named test.html be generated under the test directory:
 
<?php
 $title = "拓迈国际测试模板";
 $file = "TwoMax Inter test templet,<br>author:Matrix@Two_Max";
 $fp      = fopen ("temp.html","r");
 $content = fread ($fp,filesize ("temp.html"));
 $content .= str_replace ("{ file }",$file,$content);
 $content .= str_replace ("{ title }",$title,$content);
 // echo $content;
 
 $filename = "test/test.html";
 $handle  = fopen ($filename,"w"); //打开文件指针,创建文件
 /*
 检查文件是否被创建且可写
 */
 if (!is_writable ($filename)){
  die ("文件:".$filename."不可写,请检查其属性后重试!");
 }
 if (!fwrite ($handle,$content)){ //将信息写入文件
  die ("生成文件".$filename."失败!");
 }
 fclose ($handle); //关闭指针
 
 die ("创建文件".$filename."成功!");
?>