教你如何使用ob函數輸出靜態html檔的方法

巴扎黑
發布: 2023-03-13 15:52:01
原創
1542 人瀏覽過

如何使用ob函數輸出靜態html檔

1、ob函數介紹

#1.1、ob_start — 開啟輸出控制緩衝

bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $ ) 輸出當輸出緩衝啟動後,腳本將不會輸出內容(除http標頭外),相反需要輸出的內容儲存在內部緩衝區中。
詳情參考:

1.2、ob_get_contents — 回傳輸出緩衝區的內容

#string ob_get_contents ( void )只是得到輸出緩衝區的內容,但不清除它。
詳情參考:

#1.3、 ob_end_flush — 沖刷出(送出)輸出緩衝區內容並關閉緩衝




##bool ob_end_flush ( void )這個函數會送出最頂層緩衝區的內容(如果裡面有內容的話),並且關閉緩衝區。如果想要進一步處理緩衝區中的內容,則必須在ob_end_flush()之前呼叫 ob_get_contents(),因為在呼叫ob_end_flush()後緩衝區內容被丟棄。 詳情參考:



1.4、ob_flush — 沖刷出(送出)輸出緩衝區中的內容



#void 
ob_flush ( 
void )
這個函數會送出緩衝區的內容(如果裡面有內容的話)。如果想要進一步處理緩衝區中的內容,則必須在ob_flush()之前呼叫ob_get_contents() ,因為在呼叫ob_flush()之後緩衝區內容將會被丟棄。
此函數不會銷毀輸出緩衝區,而像ob_end_flush() 函數會銷毀緩衝區。
詳情參考:

1.5、ob_get_clean — 取得目前緩衝區的內容並刪除目前輸出緩#string 
ob_get_clean ( ### void )###得到目前緩衝區的內容並刪除目前輸出緩衝區。 ###ob_get_clean() 實質上是一起執行了 ob_get_contents() 和 ob_end_clean()。 ###詳情參考:############################################ ######################1.6、ob_get_flush — 刷出(送出)緩衝區內容,以字串形式傳回內容,並關閉輸出緩衝區### ########################################################### ################string ###ob_get_flush ( ###void )###ob_get_flush() 刷出(送出)緩衝區內容,以字串形式傳回內容,並關閉輸出緩衝區。
Note: 這個函數與ob_end_flush()相似,不同的是本函數也會以字串形式傳回緩衝區內容。
詳情參考:

#2、如何使用ob()函數來製作html的靜態頁面

2.1、簡單輸出html檔











#ob_start(); //開啟緩衝區
$info = 'hello world! ! ';
$file=fopen('index.html','w'); //開啟檔案index.html
fwrite($file,$info); //寫入訊息到index.html
fclose($file); //關閉檔案index.html
?>
輸出hello到index.html
找到index.html,正常輸出了設定的內容

2.2、取得資料庫資訊輸出html檔













require_once 'coon.php';
$sql = "select * from name order by id;";
$result = $ link->query($sql);
$arr = array();
while($re = $result->fetch(PDO::FETCH_ASSOC)){ 
$arr[] = $re;

//循環輸出內容到html檔案
ob_start(); //開啟緩衝區 
?>