一、使用fopen()函數
此函數主要傳入的是頭兩個參數(第一個是文件地址,第二個是打開方式),可以讀取任何一個文本文件,然后用while將fopen函數讀取到的每一行數據循環輸出。
如:
$file = fopen('demo.html','r');
if($file){
while(!feof($file)){
$line = fgetc($file);
echo $line;
}
}
fclose($file);
二、使用file_get_contents()函數
此函數可以將整個文件讀入一個字符串內。
如:
$string = file_get_contents("demo.html");
echo $string;
三、使用require()方法
這個函數通常放在php程序的最前面,php程序執行前,就會先讀入require所指定的文件。是它變成php程序的一部分。
require()函數沒有返回值
require的文件存在錯誤的話,程序就會終端執行,並顯示錯誤。
如:
reqire("demo.html");
四、使用include函數
這個函數一般是放在php程序中,php程序在讀取到include文件時,才將它讀取進來。
include()函數有返回值(我var_dump出來發現是int型)
include的文件存在錯誤的話,那么程序不會中斷,而是繼續執行,並顯示錯誤。
如:
include("demo.html");