欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)頁面純靜態(tài)的實(shí)例代碼

 更新時(shí)間:2017年06月21日 17:13:04   作者:學(xué)習(xí)筆記666  
本篇文章主要介紹了php實(shí)現(xiàn)頁面純靜態(tài)的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

1.先來看下面代碼index.PHP

<?php

// 準(zhǔn)備要展示到網(wǎng)頁的數(shù)據(jù)
$data = array( 
  array('id'=>1,'msg'=>'hello java'),
  array('id'=>2,'msg'=>'hello php'),
  array('id'=>3,'msg'=>'hello python'),
);

// 渲染到模板
// 實(shí)際項(xiàng)目一般是在html里渲染
// 這里演示 希望能看懂
foreach($data as $item){
  echo $item['id'].'===>'.$item['msg'].'<br/>';
}

我們可以想象訪問index.php是什么一個(gè)頁面效果,但是這個(gè)可不是我們想要的純靜態(tài)頁面哦。

我們已經(jīng)學(xué)過了php實(shí)現(xiàn)頁面靜態(tài)化的原理: http://www.dbjr.com.cn/article/116811.htm

下面來實(shí)現(xiàn)一下,看看需要改動哪些代碼。

<?php

// 準(zhǔn)備要展示到網(wǎng)頁的數(shù)據(jù)
$data = array( 
  array('id'=>1,'msg'=>'hello java'),
  array('id'=>2,'msg'=>'hello php'),
  array('id'=>3,'msg'=>'hello python'),
);

// 渲染到模板
// 實(shí)際項(xiàng)目一般是在html里渲染
// 這里演示 希望能看懂

ob_start(); // 開始輸入緩沖控制

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

// 開始生成靜態(tài)頁面文件
if(file_put_contents('index.html',ob_get_contents())){
  echo 'success';
}else{
  echo 'error';
}

執(zhí)行之后,就會生個(gè)一個(gè)index.html文件了,這就是我們真正需要的靜態(tài)頁面。

index.html內(nèi)容如下:

1===>hello java<br/>2===>hello php<br/>3===>hello python<br/>

然后我們在瀏覽器訪問index.html和最初訪問index.php顯示的內(nèi)容一樣,但是區(qū)別是index.html是靜態(tài)頁面。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。/

相關(guān)文章

最新評論