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

PHP code realizes pure static page

小云云
Release: 2023-03-20 09:38:02
Original
1333 people have browsed it

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;;
}
Copy after login

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

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