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

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

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

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

<?php

// 準備要展示到網(wǎng)頁的數(shù)據(jù)
$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/>';
}

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

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

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

<?php

// 準備要展示到網(wǎng)頁的數(shù)據(jù)
$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/>';
}

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

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

index.html內容如下:

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。/

相關文章

最新評論