include() 或 require() 函數(shù),您可以在服務(wù)器執(zhí)行 PHP 文件之前在該文件中插入一個(gè)文件的內(nèi)容,除了它們處理錯(cuò)誤的方式不同之外,這兩個(gè)函數(shù)在其他方面都是相同的.
include() 或 require() 函數(shù),您可以在服務(wù)器執(zhí)行 php 文件之前在該文件中插入一個(gè)文件的內(nèi)容,除了它們處理錯(cuò)誤的方式不同之外,這兩個(gè)函數(shù)在其他方面都是相同的,include() 函數(shù)會(huì)生成一個(gè)警告(但是腳本會(huì)繼續(xù)執(zhí)行),而 require() 函數(shù)會(huì)生成一個(gè)致命錯(cuò)誤(fatal error)(在錯(cuò)誤發(fā)生后腳本會(huì)停止執(zhí)行).
- <html>
- <body>
- <?php include("header.php"); ?>
- <h1>welcome to my home page</h1>
- <p>some text</p>
- </body>
- </html>
三個(gè)文件,"default.php"、"about.php" 以及 "contact.php" 都引用了 "menu.php" 文件,這是 "default.php" 中的代碼:
- <?php include("menu.php"); ?>
- <h1>welcome to my home page</h1>
- <p>some text</p>
- </body>
- </html>
require() 函數(shù)
require() 函數(shù)與 include() 相同,不同的是它對(duì)錯(cuò)誤的處理方式.
include() 函數(shù)會(huì)生成一個(gè)警告(但是腳本會(huì)繼續(xù)執(zhí)行),而 require() 函數(shù)會(huì)生成一個(gè)致命錯(cuò)誤(fatal error)(在錯(cuò)誤發(fā)生后腳本會(huì)停止執(zhí)行).
如果在您通過 include() 引用文件時(shí)發(fā)生了錯(cuò)誤,會(huì)得到類似下面這樣的錯(cuò)誤消息:
- <html>
- <body>
- <?php
- include("wrongfile.php");
- echo "hello world!";
- ?>
- </body>
- </html>
錯(cuò)誤消息:
新聞熱點(diǎn)
疑難解答