Home  >  Article  >  Backend Development  >  PHP code realizes pure static page

PHP code realizes pure static page

小云云
小云云Original
2018-02-12 10:27:171273browse

This article mainly introduces you to 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 you a reference. Let’s follow the editor to take a look, I hope it can help everyone.

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;;
}

We can imagine the page effect of accessing index.php, but this 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 take a look 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;;
}

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 java076402276aae5dbec7f672f8f4e5cc812===>hello php076402276aae5dbec7f672f8f4e5cc813== =>hello python076402276aae5dbec7f672f8f4e5cc81

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

Related recommendations:

php page staticization—the principle of realizing pure static page

The above is the detailed content of PHP code realizes pure static page. For more information, please follow other related articles on the PHP Chinese website!

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