php關於require和include的區別

來源:互聯網
上載者:User

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() 引用檔案時發生了錯誤,會得到類似下面這樣的錯誤訊息:

php 代碼:

<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!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.