Home > Backend Development > PHP Tutorial > Use php to generate static html pages (general 2 methods), 2 static methods_PHP tutorial

Use php to generate static html pages (general 2 methods), 2 static methods_PHP tutorial

WBOY
Release: 2016-07-12 09:08:27
Original
925 people have browsed it

Use php to generate static html pages (general 2 methods), 2 static methods

Because every time a user clicks on a dynamic link, a data query request will be sent to the server

For a website with potentially millions of visits, this is undoubtedly a huge burden on the server

So converting dynamic data into static html pages has become the first choice to save manpower and material resources

Because I had no corresponding experience before, I thought this technology was very mysterious at first

But after looking at some examples, I found that it is not that complicated (but the information on the Internet is not particularly detailed)

After a morning and afternoon experiment, I finally completed the task. Here are some thoughts and a simple example

I hope the prawns won’t laugh at you

<p> </p>
<p>一般来说 用php转换输出html页面有两种办法 引用大虾的文章如下:</p>
<p>第一种:利用模板。目前PHP的模板可以说是很多了,有功能强大的smarty,还有简单易用的smarttemplate等。它们每一种模板,都有一个获取输出内容的函数。我们生成静态页面的方法,就是利用了这个函数。用这个方法的优点是,代码比较清晰,可读性好。</p>
Copy after login

Here I use smarty as an example to illustrate how to generate a static page:

<p> </p>
<p>第二种方法:利用ob系列的函数。这里用到的函数主要是 ob_start(), ob_end_flush(), ob_get_content(),其中ob_start()是打开浏览器缓冲区的意思,打开缓冲后,所有来自PHP程序的非文件头信息均不会发送,而是 保存在内部缓冲区,直到你使用了ob_end_flush().而这里最重要的一个函数,就是ob_get_contents(),这个函数的作用是获取 缓冲区的内容,相当于上面的那个fetch(),道理一样的。</p>
Copy after login

The second method I chose is to use the ob series of functions

I was a little confused when I first read this. Later I learned that ob means output buffering, which is output buffering

When you are ready to output, all the data is saved in ob. After the server parses the php, all the html codes to be output to the client are stored in ob. If we want to output an html static page, we only need to take out the cache and write it. Just one html page

So the principle is actually very simple

Several functions are used here. Since I am new to PHP and I don’t understand many functions, I will explain them here. I hope it can help everyone

<p> </p>
<p>ob_start():开始&ldquo;捕捉&rdquo;缓存 也就是从这里开始 打开浏览器的缓存</p>
<p>ob_end_flush():关闭浏览器缓存</p>
<p>ob_get_content():读取缓存内容</p>
<p>fopen(&rdquo;文件路径&rdquo;,&rdquo;打开模式&rdquo;)打开文件 这个函数的打开模式有好几种 下面介绍几种主要的模式:</p>
<p>&ldquo;r&rdquo; 只读方式打开,将文件指针指向文件头。</p>
<p>&ldquo;r+&rdquo; 读写方式打开,将文件指针指向文件头。</p>
<p>&ldquo;w&rdquo; 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。</p>
<p>&ldquo;w+&rdquo; 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。</p>
<p>fwrite(&rdquo;文件名称&rdquo;,&rdquo;写入内容&rdquo;) 写入文件</p>
<p>fclose() 关闭文件</p>
Copy after login

Since there are many html files I want to convert, there may be hundreds of them, so the path to fopen cannot be statically specified here. You can set a path variable to save the id and other information sent by the user to facilitate the naming of html files. Here is what I combined A simple example of reading xml data in php last time

Reprint: http://www.cnblogs.com/awinlei/archive/2013/03/04/2942962.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1054357.htmlTechArticleUse php to generate static html pages (generally 2 methods), static 2 because each time the user clicks on the dynamic link The request for data query to the server is always made. For one visit, the number of visits may reach hundreds...
Related labels:
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