How to implement pure static page in php

墨辰丷
Release: 2023-03-27 08:08:02
Original
1848 people have browsed it

This article mainly introduces the example code of pure static page in PHP. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look

1. First look at the following code index.PHP

<?php

// 准备要展示到网页的数据
$data = array( 
  array(&#39;id&#39;=>1,&#39;msg&#39;=>&#39;hello java&#39;),
  array(&#39;id&#39;=>2,&#39;msg&#39;=>&#39;hello php&#39;),
  array(&#39;id&#39;=>3,&#39;msg&#39;=>&#39;hello python&#39;),
);

// 渲染到模板
// 实际项目一般是在html里渲染
// 这里演示 希望能看懂
foreach($data as $item){
  echo $item[&#39;id&#39;].&#39;===>&#39;.$item[&#39;msg&#39;].&#39;<br/>&#39;;
}
Copy after login

We can imagine accessing index.php What kind of page effect is it, but this is not the purely static page we want.

We have already learned the principle of page staticization in PHP: http://www.jb51.net/article/116811.htm

Let’s implement it and see. What code needs to be changed.

<?php

// 准备要展示到网页的数据
$data = array( 
  array(&#39;id&#39;=>1,&#39;msg&#39;=>&#39;hello java&#39;),
  array(&#39;id&#39;=>2,&#39;msg&#39;=>&#39;hello php&#39;),
  array(&#39;id&#39;=>3,&#39;msg&#39;=>&#39;hello python&#39;),
);

// 渲染到模板
// 实际项目一般是在html里渲染
// 这里演示 希望能看懂

ob_start(); // 开始输入缓冲控制

foreach($data as $item){
  echo $item[&#39;id&#39;].&#39;===>&#39;.$item[&#39;msg&#39;].&#39;<br/>&#39;;
}

// 开始生成静态页面文件
if(file_put_contents(&#39;index.html&#39;,ob_get_contents())){
  echo &#39;success&#39;;
}else{
  echo &#39;error&#39;;
}
Copy after login

After execution, an index.html file will be generated. This is the static page we really need.

index.html content is as follows:

1===>hello java
2===>hello php
3== =>hello python

Then when we access index.html in the browser, the content displayed is the same as when we initially accessed index.php, but the difference is that index.html is a static page.

The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php code implementationPurely static page

php page staticization - the principle of achieving pure static

## Static to KVDB_html/css_WEB-ITnose

The above is the detailed content of How to implement pure static page in php. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!