Home > Article > Backend Development > PHP code realizes pure static page
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('id'=>1,'msg'=>'hello java'), array('id'=>2,'msg'=>'hello php'), array('id'=>3,'msg'=>'hello python'), ); // 渲染到模板 // 实际项目一般是在html里渲染 // 这里演示 希望能看懂 foreach($data as $item){ echo $item['id'].'===>'.$item['msg'].'<br/>'; }
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('id'=>1,'msg'=>'hello java'), array('id'=>2,'msg'=>'hello php'), array('id'=>3,'msg'=>'hello python'), ); // 渲染到模板 // 实际项目一般是在html里渲染 // 这里演示 希望能看懂 ob_start(); // 开始输入缓冲控制 foreach($data as $item){ echo $item['id'].'===>'.$item['msg'].'<br/>'; } // 开始生成静态页面文件 if(file_put_contents('index.html',ob_get_contents())){ echo 'success'; }else{ echo 'error'; }
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!