php關于require和include的區別
來源:程序員人生 發布時間:2014-03-06 22:31:36 閱讀次數:3002次
include() 或 require() 函數,您可以在服務器執行 PHP 文件之前在該文件中插入一個文件的內容,除了它們處理錯誤的方式不同之外,這兩個函數在其他方面都是相同的.
include() 或 require() 函數,您可以在服務器執行 php 文件之前在該文件中插入一個文件的內容,除了它們處理錯誤的方式不同之外,這兩個函數在其他方面都是相同的,include() 函數會生成一個警告(但是腳本會繼續執行),而 require() 函數會生成一個致命錯誤(fatal error)(在錯誤發生后腳本會停止執行).
- <html>
- <body>
- <?php include("header.php"); ?>
- <h1>welcome to my home page</h1>
- <p>some text</p>
- </body>
- </html>
三個文件,"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() 函數
require() 函數與 include() 相同,不同的是它對錯誤的處理方式.
include() 函數會生成一個警告(但是腳本會繼續執行),而 require() 函數會生成一個致命錯誤(fatal error)(在錯誤發生后腳本會停止執行).
如果在您通過 include() 引用文件時發生了錯誤,會得到類似下面這樣的錯誤消息:
- <html>
- <body>
- <?php
- include("wrongfile.php");
- echo "hello world!";
- ?>
- </body>
- </html>
錯誤消息:
warning: include(wrongfile.php) [function.include]:
failed to open stream:
no such file or directory in c:homewebsitetest.php on line 5
warning: include() [function.include]:
failed opening 'wrongfile.php' for inclusion
(include_path='.;c:php5pear')
in c:homewebsitetest.php on line 5
hello world!
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈