在其他文件,包括要求的文件
通常你想要PHP代碼創建單獨的文件在不同的任務。 例如,你可能訪問一個文件 database.php ,數據庫處理用戶和最終的主網頁的index.php 另一個文件users.php。
在PHP文件可以包含在主要使用include或 require功能的頁面。
這些功能都在下面的例子可以互換的,有一點不同的解釋如下。 功能包括文件,因此,當 PHP文件的評估與包括這似乎是一個文件,因此功能都彼此可見,不含任何面向對象的問題。
php file PHP文件數據庫。
php <?PHP
connect ( ) 功能連接()
{
connects to the database / /連接到數據庫
}
?>
php file用戶。PHP文件
php <?PHP
isUser ( $ username ) 功能isUser(用戶名)
{
checks if a username is an existing user . / /檢查一個用戶名,如果是現有的用戶。
}
?>
php file索引。PHP文件
php <?PHP
include the database file / / 包括數據庫文件
' database . php ' ) ; (“數據庫PHP。);
include the users file / / 包括用戶文件
' users . php ' ) ; 包括(“用戶PHP。');
login ( $ username , $ password ) 功能登錄( 元用戶名,密碼)
{
This function was present in the database . php file / /這個函數是在數據庫中。PHP文件
連接();
This function was present in the database . php file / /這個函數是在數據庫中。PHP文件
isUser ( $ username ) ) (isUser(用戶名))
{
Carry on logging in / /進行登錄
}
}
?>
正如你可以看到上面包括功能包括在index.php文件中的文件,讓 index.php文件使用database.php和users.php功能。 當然,任何項目的變量,類或database.php和users.php功能等,可使用的功能。
' user2 . php ' ) ; 要求(“user2的PHP。”);
這裡的
user2.php文件不存在,但使用
需要的功能是因為它會拋出一個致命的錯誤
,文件要求的功能表明,這個文件需要user2.php的文件exectute。 但是,
如果包括使用它只會返回一個警告文件是不需要執行。 可能出現的另一個問題是,如果一個文件被包含在一個子文件是包含在父文件,它可能會導致一個錯誤,所以下面我們有發生這種情況的一個例子。
php file PHP文件數據庫。
php <?PHP
/ *
The rest of the database file *其餘的數據庫文件
/ *
?>
php file用戶。PHP文件
php <?PHP
include the database file / / 包括數據庫文件
' database . php ' ) ; (“數據庫PHP。);
/ *
The rest of the users file *其餘的用戶文件
/ *
?>
php file索引。PHP文件
php <?PHP
include the users file / / 包括用戶文件
' users . php ' ) ; 包括(“用戶PHP。');
Its included again ! ! / /再次包含!
' database . php ' ) ; (“數據庫PHP。);
/ *
The rest of the index . php file *其餘的指數。PHP文件
* /
?>
因為 database.php文件已被列入了兩次,一次在users.php文件中,一旦在index.php文件中,PHP會拋出一個錯誤。
確保文件只包括或要求,一旦
為了確保文件只包括或要求,一旦你需要使用
和include_once或
require_once這些文件將只包含一個文件,曾經多少次文件是在兒童檔案的
問題。這應該永遠使用,如果有機會
一個文件可以包含多次。例如...
php file PHP文件數據庫。
php <?PHP
/ *
The rest of the database file *其餘的數據庫文件
/ *
?>
php file用戶。PHP文件
php <?PHP
include the database file / / 包括數據庫文件
' database . php ' ) ; 和include_once(“PHP數據庫。');
/ *
The rest of the users file *其餘的用戶文件
/ *
?>
php file索引。PHP文件
php <?PHP
include the users file / / 包括用戶文件
' users . php ' ) ; 和include_once(“用戶PHP。');
Its included again ! ! / /再次包含!
' database . php ' ) ; 和include_once(“PHP數據庫。');
/ *
The rest of the index . php file *其餘的指數。PHP文件
* /
?>
當 index.php文件是評估它不會拋出一個錯誤,因為 database.php文件只包含一次users.php文件users.php是唯一一次在index.php文件中包含。 作為一個規則,它通常總是使用
的和 include_once
和 require_once
功能 ,
包括和需要的功能,而不是一個更好的主意。
最新的評論:1。 第1頁共1頁。 平均評分:

11:09am on Friday, July 15th, 2011 劉慧卿 上週五上午11:09,7月15日,2011年
作者由Dominic斯金納
最後更新:2011年10月25日16點00分38秒