php代碼實現頁面純靜態

來源:互聯網
上載者:User
本文主要和大家介紹了php實現頁面純靜態執行個體代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能協助到大家。

1.先來看下面代碼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/>';}

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

我們已經學過了php實現頁面靜態化的原理: http://www.jb51.net/article/116811.htm

下面來實現一下,看看需要改動哪些代碼。


<?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';}

執行之後,就會生個一個index.html檔案了,這就是我們真正需要的靜態頁面。

index.html內容如下:

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

然後我們在瀏覽器訪問index.html和最初訪問index.php顯示的內容一樣,但是區別是index.html是靜態頁面。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.