Home  >  Article  >  Backend Development  >  How to generate static pages using PHP language?

How to generate static pages using PHP language?

WBOY
WBOYOriginal
2016-07-25 08:46:241036browse

In terms of PHP web development and search engine optimization website promotion needs, the space station website or local electrostatic treatment is required. There are many ways to generate static HTML pages in PHP, such as using PHP templates and caching static pages to achieve it today. Go to php tutorial to discuss how to generate static pages in PHP. There are two types of static page methods, one is pseudo-static, which is URL rewriting, and the other is your real static. Here, the editor explains how to create static pages in PHP.



What is PHP staticization

The simple understanding of PHP staticization is to make the website generated pages displayed in front of visitors in the form of static HTML. PHP staticization is divided into pure staticization and pseudo-staticization. The difference between the two The difference lies in the processing mechanism of PHP generating static pages.

How to generate static HTML pages with PHP

1. Use PHP templates to generate static pages -php learning>>
It is very convenient to achieve staticization of PHP templates, such as installing and using PHP Smarty to achieve static website.


2. Use PHP file reading and writing functions to generate static pages
PHP generates static page example code

$out1 = "
Welcome to the PHP website development tutorial website. This article mainly introduces the method of staticizing PHP website pages
";
$fp = fopen("leapsoulcn.html","w");
if(!$fp)
{
echo "System Error";
exit();
}
else {
fwrite($fp,$out1);
fclose($fp);
echo "Success";
}
?>

3, PHP example tutorial-Use PHP output control function (Output Control) to generate static pages
The output control function (Output Control) uses and controls the cache to generate static HTML pages, and also uses PHP file reading and writing functions.
PHP generates static page example code

ob_start();
echo "".
"".
"".
"".
"Welcome to the PHP website development tutorial website. This article mainly introduces the method of staticizing PHP website pages".
"";
$out1 = ob_get_contents();
ob_end_clean();
$fp = fopen("leapsoulcn.html","w");
if(!$fp)
{
echo "System Error";
exit();
}
else
{
fwrite($fp,$out1);
fclose($fp);
echo "Success";
}
?>

We know that PHP is used for website development in web development. Generally, the execution results are directly output to the browser. In order to use PHP to generate static pages, you need to use the output control function to control the cache area in order to obtain the cache area. The content is then output to a static HTML page file to achieve static website.

The idea of ​​generating a static page in PHP is: first turn on the cache, and then output the HTML content (you can also include the HTML content in the form of a file through include), then obtain the content in the cache, clear the cache and pass the PHP file The read and write functions write the cached content into the static HTML page file. Tutorial on reading and writing PHP files?

The process of obtaining the output cache content to generate a static HTML page requires the use of three functions: ob_start(), ob_get_contents(), ob_end_clean().

Knowledge points:
1. The ob_start function is generally used to enable caching. Note that there cannot be any output before using ob_start, such as spaces, characters, etc.

2. The ob_get_contents function is mainly used to obtain the content in the cache and return it in the form of a string. Note that this function must be called before the ob_end_clean function, otherwise the cache content cannot be obtained.

3. The ob_end_clean function mainly clears the contents of the cache and closes the cache. It returns True if successful and False if failed.

PHP Output Control function has many applications and will be expanded in the future.

The above php example tutorial is an example. The method of using PHP to generate static HTML pages to achieve website staticization will definitely be used in work. I hope scholars can practice more offline. You can choose different static methods according to the actual situation and needs. More php video tutorials courses are welcome to learn and watch online! www.kokojia.com




Statement:
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