Php 简明教程
PHP - File Inclusion
你可以在服务器执行之前将一个 PHP 文件的内容包含到另一个 PHP 文件中。有两个 PHP 函数可用于将一个 PHP 文件包含到另一个 PHP 文件中。
You can include the content of a PHP file into another PHP file before the server executes it. There are two PHP functions which can be used to included one PHP file into another PHP file.
-
The include() Function
-
The require() Function
这是 PHP 的一个优点,它有助于创建可以在多个页面中重复使用的函数、页眉、页脚或元素。这将有助于开发人员毫不费力地轻松更改整个网站的布局。如果有任何更改需求,则无需更改数千个文件,只需要更改包含的文件即可。
This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file.
The include() Function
include() 函数获取指定文件中所有的文本并且将其复制到使用 include 函数的文件中。如果加载文件时有任何问题,则 include() 函数会生成警告,但是脚本将继续执行。
The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
假设你要为网站创建一个通用菜单。然后创建一个包含以下内容的文件 menu.php。
Assume you want to create a common menu for your website. Then create a file menu.php with the following content.
<a href="http://www.tutorialspoint.com/index.htm">Home</a>
<a href="http://www.tutorialspoint.com/ebxml">ebXML</a>
<a href="http://www.tutorialspoint.com/ajax">AJAX</a>
<a href="http://www.tutorialspoint.com/perl">PERL</a>
现在创建尽可能多的页面,并添加此文件以创建页眉。例如,现在你的 test.php 文件可以包含以下内容。
Now create as many pages as you like and include this file to create header. For example now your test.php file can have following content.
<?php <b>include("menu.php");</b> ?>
<p>This is an example to show how to include PHP file!</p>
它将产生以下结果 −
It will produce the following result −
The require() Function
require() 函数获取指定文件中所有的文本并且将其复制到使用 include 函数的文件中。如果加载文件时有任何问题,则 require() 函数会生成致命错误并停止脚本执行。
The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.
因此 require() 和 include() 之间的区别在于它们如何处理错误条件。建议使用 require() 函数,而不要使用 include() 函数,因为如果缺少文件或文件命名错误,脚本不应继续执行。
So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
你可以尝试使用以上示例结合 require() 函数,它会生成相同的结果。但是,如果将尝试以下两个示例,其中文件不存在,你将会得到不同的结果。
You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.
<?php include("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
这会产生以下结果 −
This will produce the following result −
This is an example to show how to include wrong PHP file!
现在我们尝试使用 require() 函数来执行相同的示例。
Now lets try same example with require() function.
<?php <b>require("xxmenu.php");</b> ?>
<p>This is an example to show how to include wrong PHP file!</p>
这次文件执行停止,没有显示任何东西。
This time file execution halts and nothing is displayed.
NOTE − 你可能会得到普通警告消息或致命错误消息,或者根本没有消息。这取决于你的 PHP 服务器配置。
NOTE − You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.