Php 简明教程
PHP - File Include
PHP 中的 include statement 类似于 Java 或 Python 中的 import statement 、C/C++ 中的 #include directive 。但是,在 PHP 中包含语句工作方式上略有不同。
The include statement in PHP is similar to the import statement in Java or Python, and #include directive in C/C++. However, there is a slight difference in the way the include statement works in PHP.
Java/Python 中的导入或 C/C++ 中的 #include 只将一个或多个语言结构(例如函数或类)从一个文件中加载到当前文件中。相比之下,PHP 中的 include 语句将另一个文件中的一切引入现有 PHP 脚本。它可能是 PHP 代码、文本文件、HTML 标记等。
The Java/Python import or #include in C/C++ only loads one or more language constructs such as the functions or classes defined in one file into the current file. In contrast, the include statement in PHP brings in everything in another file into the existing PHP script. It may be a PHP code, a text file, HTML markup, etc.
The "include" Statement in PHP
以下是 PHP 中 include 语句如何工作的一个典型示例:
Here is a typical example of how the include statement works in PHP −
test.php
<?php
include 'myfile.php';
# PHP script in test.php
?>
PHP 中的 include 关键字非常方便,尤其当你需要在项目中跨多个 PHP 脚本使用相同的 PHP 代码(函数或类)或 HTML 标记时。一个典型的例子是创建应出现在 Web 应用程序所有页面中的菜单。
The include keyword in PHP is very handy, especially when you need to use the same PHP code (function or class) or HTML markup across multiple PHP scripts in a project. A case in point is the creation of a menu that should appear across all pages of a web application.
假设你想为网站创建一个通用菜单。然后,创建一个名为“menu.php”的文件,其内容如下。
Suppose 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> <br />
现在创建任意多页面并包含此文件以创建头部。例如,现在你的“test.php”文件可以具有以下内容:
Now create as many pages as you like and include this file to create the header. For example, now your "test.php" file can have the following content −
<html>
<body>
<?php include("menu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>
这两个文件假定存在于 XAMPP 服务器的文档根目录中。访问 http://localhost/test.php URL。它将生成以下内容 output :
Both the files are assumed to be present in the document root folder of the XAMPP server. Visit http://localhost/test.php URL. It will produce the following output −
当 PHP 解析器遇到 include 关键字时,它会尝试在当前脚本正在执行的同一目录中找到指定文件。如果未找到,则搜索“php.ini”的“include_path”设置中的目录。
When PHP parser encounters the include keyword, it tries to find the specified file in the same directory from which the current script is being executed. If not found, the directories in the "include_path" setting of "php.ini" are searched.
当包含一个文件时,它包含的代码会继承包含所在行可变的范围。呼叫文件中该行可用的任何变量都将在从该点开始的呼叫文件中可用。但是,在包含文件中定义的所有函数和类都具有全局范围。
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
Example
在以下示例中,我们有一个“myname.php”脚本,其中声明了两个变量。它包含在另一个脚本 test.php 中。变量以全局范围加载。
In the following example, we have a "myname.php" script with two variables declared in it. It is included in another script test.php. The variables are loaded in the global scope.
myname.php
myname.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
test.php
<?php
include "myname.php";
echo "<h2>$fname $lname</h2>";
?>
当浏览器访问 http://localhost/test.php 时,它显示:
When the browser visits http://localhost/test.php, it shows −
Ravi Teja
然而,如果文件包含在函数内部,则变量仅成为函数局部范围的一部分。
However, if the file is included inside a function, the variables are a part of the local scope of the function only.
myname.php
myname.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
test.php
<?php
function showname() {
include "myname.php";
}
echo "<h2>$fname $lname</h2>";
?>
现在当浏览器访问 http://localhost/test.php 时,它显示未定义的变量警告:
Now when the browser visits http://localhost/test.php, it shows undefined variable warnings −
Warning: Undefined variable $fname in C:\xampp\htdocs\test.php on line 7
Warning: Undefined variable $lname in C:\xampp\htdocs\test.php on line 7
include_once statement
与 include 一样,PHP 也有“ include_once ”关键字。唯一的区别在于,如果已包含来自文件中的代码,则不会再次包含,且 “include_once” 返回 true。正如名称所建议的,文件将只包含一次。
Just like include, PHP also has the "include_once" keyword. The only difference is that if the code from a file has already been included, it will not be included again, and "include_once" returns true. As the name suggests, the file will be included just once.
“include_once” 可能在以下情况下使用:同一文件可能在脚本的特定执行过程中包含并评估多次,因此它可以帮助避免重新定义函数、重新赋值变量值等问题。
"include_once" may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so it can help avoid problems such as function redefinitions, variable value reassignments, etc.
PHP – Include vs Require
PHP 中的 require 关键字与 include 关键字非常相似。两者的区别在于,失败后 require 会产生致命的 E_COMPILE_ERROR 级别的错误。
The require keyword in PHP is quite similar to the include keyword. The difference between the two is that, upon failure require will produce a fatal E_COMPILE_ERROR level error.
换句话说, require 会停止脚本,而 include 只会发出警告(E_WARNING),允许脚本继续运行。
In other words, require will halt the script, whereas include only emits a warning (E_WARNING) which allows the script to continue.
require_once keyword
“require_once”关键字与 require 类似,但也有细微差别。如果您使用“require_once”,那么 PHP 将检查文件是否已包含,如果是,则不会再包含相同的文件。
The "require_once" keyword is similar to require with a subtle difference. If you are using "require_once", then PHP will check if the file has already been included, and if so, then the same file it will not be included again.