1.先來(lái)看下面代碼index.PHP
<?php // 準(zhǔn)備要展示到網(wǎng)頁(yè)的數(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/>'; }
我們可以想象訪問(wèn)index.php是什么一個(gè)頁(yè)面效果,但是這個(gè)可不是我們想要的純靜態(tài)頁(yè)面哦。
我們已經(jīng)學(xué)過(guò)了php實(shí)現(xiàn)頁(yè)面靜態(tài)化的原理: http://www.gimoo.net/article/116811.htm
下面來(lái)實(shí)現(xiàn)一下,看看需要改動(dòng)哪些代碼。
<?php // 準(zhǔn)備要展示到網(wǎng)頁(yè)的數(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(); // 開(kāi)始輸入緩沖控制 foreach($data as $item){ echo $item['id'].'===>'.$item['msg'].'<br/>'; } // 開(kāi)始生成靜態(tài)頁(yè)面文件 if(file_put_contents('index.html',ob_get_contents())){ echo 'success'; }else{ echo 'error'; }
執(zhí)行之后,就會(huì)生個(gè)一個(gè)index.html文件了,這就是我們真正需要的靜態(tài)頁(yè)面。
index.html內(nèi)容如下:
1===>hello java<br/>2===>hello php<br/>3===>hello python<br/>
然后我們?cè)跒g覽器訪問(wèn)index.html和最初訪問(wèn)index.php顯示的內(nèi)容一樣,但是區(qū)別是index.html是靜態(tài)頁(yè)面。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持綠夏網(wǎng)。/