---
title: "PHP中的引用文件应用（inlude，require，inlude_once，require_once）"
author: "cakin24"
date: "2016-12-30T19:30:20Z"
tags: []
categories: ["编程语言"]
slug: "php-inlude-require-inlude-once-require-once"
draft: false
lastmod: "2016-12-30T19:30:20Z"
id: 2348260
url: "https://cakin24.iteye.com/blog/2348260"
---

**一 实例**

1、index.php

01. `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"`
02. `"http://www.w3.org/TR/html4/loose.dtd">`
03. `<html>`
04. `<head>`
05. `<metahttp-equiv="Content-Type"content="text/html; charset=gb2312">`
06. `<title>应用include语句引用外部文件</title>`
07. `</head>`
08. `<body>`
09. `<tablewidth="975"border="0"cellpadding="0"cellspacing="0">`
10. `<tr>`
11. `<td><?php include("top.php");?></td>`
12. `</tr>`
13. `<tr>`
14. `<td><?php include("main.php");?></td>`
15. `</tr>`
16. `<tr>`
17. `<td><?php include("bottom.php");?></td>`
18. `</tr>`
19. `</table>`
20. `</body>`
21. `</html>`

2、top.php

01. `<html>`
02. `<head>`
03. `<metahttp-equiv="Content-Type"content="text/html; charset=gb2312">`
04. `<title></title>`
05. `<styletype="text/css">`
06. `<!--`
07. `body {`
08. `margin-top: 0px;`
09. `margin-bottom: 0px;`
10. `}`
11. `-->`
12. `</style>`
13. `</head>`
14. `<body>`
15. `<tablewidth="975"border="1"cellpadding="0"cellspacing="0">`
16. `<tr>`
17. `<td>我是TOP</td>`
18. `</tr>`
19. `</table>`
20. `</body>`
21. `</html>`

3、main.php

01. `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"`
02. `"http://www.w3.org/TR/html4/loose.dtd">`
03. `<html>`
04. `<head>`
05. `<metahttp-equiv="Content-Type"content="text/html; charset=gb2312">`
06. `<title></title>`
07. `<styletype="text/css">`
08. `<!--`
09. `body {`
10. `margin-top: 0px;`
11. `margin-bottom: 0px;`
12. `}`
13. `.style3 {font-size: 20px; font-family: "隶书"; color: #FF3366;}`
14. `-->`
15. `</style>`
16. `</head>`
17. `<body>`
18. `<tablewidth="975"border="1"cellpadding="0"cellspacing="0">`
19. `<tr>`
20. `<tdwidth="787"valign="top">`
21. `我是main`
22. `</td>`
23. `</tr>`
24. `</table>`
25. `</body>`
26. `</html>`

4、bottom.php

01. `<html>`
02. `<head>`
03. `<metahttp-equiv="Content-Type"content="text/html; charset=gb2312">`
04. `<title></title>`
05. `<styletype="text/css">`
06. `<!--`
07. `body {`
08. `margin-top: 0px;`
09. `}`
10. `-->`
11. `</style></head>`
12. `<body>`
13. `<tablewidth="975"border="1"cellpadding="0"cellspacing="0">`
14. `<tr>`
15. `<td>我是bottom</td>`
16. `</tr>`
17. `</table>`
18. `</body>`
19. `</html>`

**二 运行结果**

![](http://dl2.iteye.com/upload/attachment/0122/3267/08b275fc-0db7-3d66-82a4-9764e1be06b8.png)

**三 运行说明**

1、inlude语句和require语句的使用区别

使用require语句调用文件时，如果调用的文件没找到，require语句会输出错误信息，并且立即终止脚本的处理。而include语句在没有找到文件时则会弹出警告，不会终止脚本处理。

使用require语句调用文件时，只要程序一执行，就会立刻调用外部文件；而通过include语句调用外部文件时，只有程序执行到该语句时，才会调用外部文件。

2、inlude\_once语句和require\_once语句的使用区别

inlude\_once语句和require\_once语句的用途是确保一个被保护文件只能被包含一次。使用这两个语句可以防止意外地多次包含相同的函数库，从而导致函数的重复定义并产生错误。

但两者之间也是由区别的，inlude\_once语句在脚本执行期间调用外部文件发生错误时候，产生一个警告，而require\_once语句则导致一个致命的错误。

3、文件引用可实现网站主页灵活布局。