在PHP變成中,include()與require()的功能相同,include(include_once) 與 require(require_once)都是把把包含的文件代碼讀入到指定位置來,但是二者再用法上有區(qū)別:(include()是有條件包含函數(shù),而require()則是無條件包含函數(shù)) 
1, 使用方式不同
(1) require 的使用方法如 require("requireFile.php"); 。這個(gè)函式通常放在 PHP 程式的最前面,PHP 程式在執(zhí)行前,就會(huì)先讀入 require 所指定引入的檔案,使它變成 PHP 程式網(wǎng)頁的一部份。常用的函式,亦可以這個(gè)方法將它引入網(wǎng)頁中。引入是無條件的,發(fā)生在程序執(zhí)行前,不管條件是否成立都要導(dǎo)入(可能不執(zhí)行)。
(2) include 使用方法如 include("includeFile.php"); 。這個(gè)函式一般是放在流程控制的處理區(qū)段中。PHP 程式網(wǎng)頁在讀到 include 的檔案時(shí),才將它讀進(jìn)來。這種方式,可以把程式執(zhí)行時(shí)的流程簡(jiǎn)單化。引入是有條件的,發(fā)生在程序執(zhí)行時(shí),只有條件成立時(shí)才導(dǎo)入(可以簡(jiǎn)化編譯生成的代碼)。
例如在下面的一個(gè)例子中,如果變量$somgthing為真,則將包含文件somefile:
復(fù)制代碼 代碼如下:
if($something){
include("somefile");
}
復(fù)制代碼 代碼如下:
if($something){
require("somefile");
}
復(fù)制代碼 代碼如下:
$i = 1;
while ($i < 3) {
require("somefile.$i");
$i++;
}
復(fù)制代碼 代碼如下:
$i = 1;
while ($i < 3) {
include("somefile.$i");
$i++;
}
test1.php
復(fù)制代碼 代碼如下:
<?PHP
include  (”test3.php”);
echo  “abc”;
?>
復(fù)制代碼 代碼如下:
<?PHP
require (”test3.php”)
echo  “abc”;
?>
新聞熱點(diǎn)
疑難解答