這三個檔案, “ default.php ” , “ about.php ”和“ contact.php ”大家都應該include“ menu.php ”檔案。以下是代碼“ default.php ” :
<?php include("menu.php"); ?>
輸出.
<html><body><a href="default.php">Home</a> |<a href="about.php">About Us</a> | <a href="contact.php">Contact Us</a><h1>Welcome to my home page</h1><p>Some text</p></body></html> |
下面我們再來看看require函數www.111cn.net/
的require( )函數是相同的,include( ) ,只是不同的處理錯誤。
在include( )函數產生一個警告(但該指令碼將繼續執行) ,而需要( )函數產生一個致命的錯誤(和指令碼執行後,將停止錯誤) 。
如果您加入了檔案,include( )函數和發生錯誤時,你可能得到一個錯誤資訊類似下面的一個。
<?php
include("wrongFile.php");
echo "Hello World!";
?>
Warning: include(wrongFile.php) [function.include]:failed to open stream:No such file or directory in C:homewebsiteest.php on line 5
Warning: include() [function.include]:Failed opening 'wrongFile.php' for inclusion(include_path='.;C:php5pear')in C:homewebsiteest.php on line 5
www.111cn.net/phper/php.html
Hello World!
請注意,聲明的迴音仍是執行!這是因為警示不停止執行指令碼。
現在,讓我們運行相同的例子與require( )函數。
PHP代碼:
<?phprequire("wrongFile.php");echo "Hello World!";?>
提示:
ning: require(wrongFile.php) [function.require]:failed to open stream:No such file or directory in C:homewebsiteest.php on line 5
Fatal error: require() [function.require]:Failed opening required 'wrongFile.php'(include_path='.;C:php5pear')in C:homewebsiteest.php on line 5
轉載: www.111cn.net/phper/php.html