php实现页面纯静态的实例代码
1.先来看下面代码index.PHP
1,'msg'=>'hellojava'),
array('id'=>2,'msg'=>'hellophp'),
array('id'=>3,'msg'=>'hellopython'),
);
//渲染到模板
//实际项目一般是在html里渲染
//这里演示希望能看懂
foreach($dataas$item){
echo$item['id'].'===>'.$item['msg'].'
';
}
我们可以想象访问index.php是什么一个页面效果,但是这个可不是我们想要的纯静态页面哦。
我们已经学过了php实现页面静态化的原理:https://www.nhooo.com/article/116811.htm
下面来实现一下,看看需要改动哪些代码。
1,'msg'=>'hellojava'),
array('id'=>2,'msg'=>'hellophp'),
array('id'=>3,'msg'=>'hellopython'),
);
//渲染到模板
//实际项目一般是在html里渲染
//这里演示希望能看懂
ob_start();//开始输入缓冲控制
foreach($dataas$item){
echo$item['id'].'===>'.$item['msg'].'
';
}
//开始生成静态页面文件
if(file_put_contents('index.html',ob_get_contents())){
echo'success';
}else{
echo'error';
}
执行之后,就会生个一个index.html文件了,这就是我们真正需要的静态页面。
index.html内容如下:
1===>hellojava
2===>hellophp
3===>hellopython
然后我们在浏览器访问index.html和最初访问index.php显示的内容一样,但是区别是index.html是静态页面。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。/
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下