加載自定義的函數(shù)庫
使用自定義函數(shù)庫
?代碼重用
–通過重復(fù)使用已有的代碼,提高開發(fā)效率,降低成本
?include( )和require( )函數(shù)。
–require( )將一個文件在預(yù)處理期間被導(dǎo)入,像把該文件粘貼到使用函數(shù)的地方。
–include( )與require ( )幾乎等價,區(qū)別在于在腳本執(zhí)行時包
含,當(dāng)處理失敗時,include( )產(chǎn)生一個警告而require( )則導(dǎo)致一個致命錯誤。
?include_once( )和require_once( )函數(shù)
兩個函數(shù)在腳本執(zhí)行期間包括并運行指定文件。與include( )語句及require( )類似,唯一區(qū)別是如果該文件中的代碼已經(jīng)被包括了,則不會再次包括,只會包括一次。這兩個函數(shù)應(yīng)該用于在腳本執(zhí)行期間同一個文件有可能被包括超過一次的情況下,你想確保它只被包括一次以避免函數(shù)重定義,變量重新賦值等問題。
<?php
**require** 'config.php'; //使用require語句包含并執(zhí)行config.php文件
**if** ($condition) //在流程控制中使用include語句
**include** 'file.txt'; //使用include語句包含并執(zhí)行file.txt文件
**else** //條件不成立則包含下面的文件
**include** ('other.php'); //使用include語句包含并執(zhí)行other.php文件
**require** ('somefile.txt'); //使用require語句包含并執(zhí)行somefile.txt文件
function.php
<?php
function one() {
echo "111111111111111<br>";
}
function two() {
echo "222222222222222<br>";
}
function three() {
echo "33333333333333333<br>";
}
test.php
<?php
require "function.inc.php";
if($a == "a")
include "demo.txt";
else
include "demo2.html";
one();
two();
three();