溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php中文件包含的方式

發布時間:2021-06-03 18:09:14 來源:億速云 閱讀:175 作者:Leah 欄目:開發技術

這篇文章將為大家詳細講解有關php中文件包含的方式,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

四種語句

PHP中有四個加載文件的語句:include、require、include_once、require_once。

基本語法

require:require函數一般放在PHP腳本的最前面,PHP執行前就會先讀入require指定引入的文件,包含并嘗試執行引入的腳本文件。require的工作方式是提高PHP的執行效率,當它在同一個網頁中解釋過一次后,第二次便不會解釋。但同樣的,正因為它不會重復解釋引入文件,所以當PHP中使用循環或條件語句來引入文件時,需要用到include。

include:可以放在PHP腳本的任意位置,一般放在流程控制的處理部分中。當PHP腳本執行到include指定引入的文件時,才將它包含并嘗試執行。這種方式可以把程序執行時的流程進行簡單化。當第二次遇到相同文件時,PHP還是會重新解釋一次,include相對于require的執行效率下降很多,同時在引入文件中包含用戶自定義函數時,PHP在解釋過程中會發生函數重復定義問題。

require_once / include_once:分別與require / include作用相同,不同的是他們在執行到時會先檢查目標內容是不是在之前已經導入過,如果導入過了,那么便不會再次重復引入其同樣的內容。

相互區別

include和require:

include有返回值,而require沒有返回值。

include在加載文件失敗時,會生成一個警告(E_WARNING),在錯誤發生后腳本繼續執行。所以include用在希望繼續執行并向用戶輸出結果時。

//test1.php

<?php

include './tsest.php';

echo 'this is test1';

?>

 

//test2.php

<?php

echo 'this is test2\n';

function test() {

 echo 'this is test\n';

}

?>

 

//結果:

this is test1

require在加載失敗時會生成一個致命錯誤(E_COMPILE_ERROR),在錯誤發生后腳本停止執行。一般用在后續代碼依賴于載入的文件的時候。

//test1.php

<?php

require './tsest.php';

echo 'this is test1';

?>

 

//test2.php

<?php

echo 'this is test2\n';

function test() {

 echo 'this is test\n';

}

?>

結果:

php中文件包含的方式

include和include_once:

include載入的文件不會判斷是否重復,只要有include語句,就會載入一次(即使可能出現重復載入)。而include_once載入文件時會有內部判斷機制判斷前面代碼是否已經載入過。這里需要注意的是include_once是根據前面有無引入相同路徑的文件為判斷的,而不是根據文件中的內容(即兩個待引入的文件內容相同,使用include_once還是會引入兩個)。

//test1.php

<?php

include './test2.php';

echo 'this is test1';

include './test2.php';

?>

 

//test2.php

<?php

echo 'this is test2';

?>

 

//結果:

this is test2this is test1this is test2

 

 

//test1.php

<?php

include './test2.php';

echo 'this is test1';

include_once './test2.php';

?>

 

//test2.php

<?php

echo 'this is test2';

?>

 

//結果:

this is test2this is test1

 

 

//test1.php

<?php

include_once './test2.php';

echo 'this is test1';

include './test2.php';

?>

 

//test2.php

<?php

echo 'this is test2';

?>

 

//結果:

this is test2this is test1this is test2

 

 

//test1.php

<?php

include_once './test2.php';

echo 'this is test1';

include_once './test2.php';

?>

 

//test2.php

<?php

echo 'this is test2';

?>

 

//結果:

this is test2this is test1

關于php中文件包含的方式就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女