深入理解require与require_once与include以及include_once的区别
本篇文章是对require与require_once与include以及include_once的区别进行了详细的分析介绍,需要的朋友参考下
PHP具有快速、可靠、跨平台应用、源代码开放等特点,使得PHP成为最受欢迎的服务器端Script语言之一。我根据自己在工作中体会到的,向大家介绍PHP使用的心得,希望对大家有所帮助。
利用PHP的Include files维护你的网站
不管你所开发的网站的规模是大是小,你都应该要认识到重复使用程序代码的重要性,不论你重复使用的是 PHP 程序或者是 HTML 原始码。举个例子来说,网站页尾的版权宣告至少每年都得修改一次,如果你的网站有许多个页面,该怎么办呢?动手一个一个修改这些页面肯定是一件头痛的事情。通过 PHP 我们可以用几个不同的方式来重复使用程序代码。要使用哪些函数端视你要重复使用的是怎样的内容而定。
这些主要的函数包括:
* include() 与 include_once()
* require() 与 require_once()
1.include() 函数会将指定的档案读入并且执行里面的程序。
例如:include('/home/me/myfile');
被导入的档案中的程序代码都会被执行,而且这些程序在执行的时候会拥有和源文件中呼叫到 include() 函数的位置相同的变量范围(variable scope)。你可以导入同一个服务器中的静态档案,甚至可以通过合并使用 include() 与 fopen() 函数来导入其它服务器上面的档案。
2.include_once()函数的作用和 include() 是几乎相同的
唯一的差别在于 include_once() 函数会先检查要导入的档案是不是已经在该程序中的其它地方被导入过了,如果有的话就不会再次重复导入该档案(这项功能有时候是很重要的,比方说要导入的档案里面宣告了一些你自行定义好的函数,那么如果在同一个程序重复导入这个档案,在第二次导入的时候便会发生错误讯息,因为 PHP 不允许相同名称的函数被重复宣告第二次)。
3.require()函数会将目标档案的内容读入,并且把自己本身代换成这些读入的内容。
这个读入并且代换的动作是在 PHP 引擎编译你的程序代码的时候发生的,而不是发生在 PHP 引擎开始执行编译好的程序代码的时候(PHP 3.0 引擎的工作方式是编译一行执行一行,但是到了 PHP 4.0 就有所改变了,PHP 4.0 是先把整个程序代码全部编译完成后,再将这些编译好的程序代码一次执行完毕,在编译的过程中不会执行任何程序代码)。require() 通常来导入静态的内容,而 include() 则适合用来导入动态的程序代码。
4.如同 include_once()函数,require_once() 函数会先检查目标档案的内容是不是在之前就已经导入过了,如果是的话,便不会再次重复导入同样的内容。
我个人习惯使用 require() 函数来导入版权宣告(copyrights),静态文字或其它本身不含有变量,或者本身需要倚赖其它执行过的程序才能正确执行的程序代码。例如:
复制代码 代码如下:
[一堆内容]
// 导入版权宣告文字
require('/home/me/mycopyright');
?>
另一方面,我通常在程序的开头使用 include() 函数来导入一些函式库或者类似的程序代码:
复制代码 代码如下:
// 导入我的函式库
include('/home/me/myfunctions');
// 利用之前导入的函式库里面定义好的 PHP 函数执行一些功能
?>
[一堆内容]
接下来你可能会问这第一个挺符合逻辑的问题:「这些被导入的档案要放在哪儿呢?」简短的答案是:「放在服务器档案系统里的任何地方都行。」然而,要留意的是如果被导入的档案除了单纯的程序代码片段以外还包含了一些敏感资料,例如连结数据库系统要用到的帐号和密码,那么建议你不要把这些档案放在 Web 服务器的文件根目录之下,因为那样的话他人便可以很容易地窃取到这些资料了。
你可以将这些被包含的档案放在系统的任何一个目录里面,唯一的条件是 PHP 本身用来执行的身分(www,nobody 或者其它身分)必须要有足够的权限能够读取这些档案就可以了。这些档案的扩展名也可以任意取,甚至没有附档名也无所谓。
善用include()和 require()来将网站里面经常需要变动的共享内容做合理的分割,香港服务器,在更新网站内容的时候将会容易进行得多。
利用PHP来维护档案系统
PHP 提供了很多与档案系统相关的函数,让我们不仅可以开启档案,还能够显示目录的内容,搬移档案的位置以及其它更多功能。有的朋友甚至写了能够通过浏览器来管理档案内容的 PHP 程序。
在开始介绍 PHP 的档案系统相关功能之前,我们要先理清一件事情:在 Windows操作系统里面,美国空间,档案路径可以使用斜线(/)或者反斜线(\)来表示,但是在其它操作系统里面我们只会使用到斜线。为了保持统一性,下面的例子里面的档案路径都是使用斜线。
下面的例子程序我将教大家基本的目录内容显示功能,每个步骤都有批注,请直接阅读。
复制代码 代码如下:
/* $dir_name 这个变量的值是你想要读取的目录的完整路径 */
$dir_name = "/home/me/";
/* opendir()函数会开启某个目录,并且传回一个参考值(handle)让我们可以用来在程序中参照到该目录 */
$dir = opendir($dir_name);
/* 开始建立一个字符串,这个字符串包含了 HTML 的列表卷标,用来显示目录中的文件名称。 */
$file_list = "
- ";
- $file_name";
}
}
/* 替 HTML 列表卷标加上结尾 */
$file_list .= "
/* 使用一个 while 循环叙述将前面开启的目录中的档案全部读取一遍。如果读取到的档名不是「.」或者「..」,就把该档名写入前面提到的字符串里面去。 */
while ($file_name = readdir($dir)) {
if (($file_name != ".") && ($file_name != "..")) {
$file_list .= "
/* 关闭之前开启的目录并且结束这段 PHP 程序 */
closedir($dir);
?>
Files in: echo "$dir_name"; ?>
echo "$file_list"; ?>
经过上面几步,你已经成功把某个目录中的文件名称显示在网页上了。但你要记住一点:要读取某个目录或者档案(读取档案内容的做法稍后会介绍),PHP 本身执行所用的身分必须至少拥有该目录或者档案的读取权限才行,否则系统会显示权限不足的错误讯息。
下一个例子我将教大家如何复制一个档案:
复制代码 代码如下:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

When we write web pages using PHP, sometimes we need to include code from other PHP files in the current PHP file. At this time, you can use the include or include_once function to implement file inclusion. So, what is the difference between include and include_once?

Usage of require: 1. Introduce modules: In many programming languages, require is used to introduce external modules or libraries so that the functions they provide can be used in the program. For example, in Ruby, you can use require to load third-party libraries or modules; 2. Import classes or methods: In some programming languages, require is used to import specific classes or methods so that they can be used in the current file; 3. Perform specific tasks: In some programming languages or frameworks, require is used to perform specific tasks or functions.

If you are a PHP developer, then you must know that various errors and warnings often appear in PHP programming. One of the problems that often occurs is the "PHPWarning:require_once()" error. This article will introduce how to solve this problem. First, let’s understand what this error means. When you use require_once() function in PHP code, it tries to load the specified file. If the file does not exist, then

Steps to resolve fatalerror:require():Failedopeningrequired'data/tdk.php'(include_path='.;C:phppear') in PHP header When developing websites or applications using PHP, we often encounter various errors . One of the common errors is "fatalerror:require():Failed

Detailed explanation of the role and usage of the require keyword in PHP In PHP development, require is a very commonly used keyword. Its function is to include the specified file for use by the current script. This article will explain in detail the function and use of the require keyword. 1. The role of the require keyword The require keyword can include the contents of a file into the current script. It is usually used to include some necessary external files, such as library files, configuration files, etc. Use req

Steps to resolve FatalError:require():Failedopeningrequired'data/tdk.php' in PHP header When developing and maintaining PHP websites, we often encounter various errors and exceptions. One of the common errors is "FatalError:require():Failedopeningrequired'data/tdk.php'".

Introduction In PHP, various URL style protocols can be used in conjunction with file system functions, with the help of corresponding built-in wrappers. Custom wrappers can also be defined using the stream_wrapper_register() function. The default wrapper in PHP is file://, which represents the local file system. If no other protocol is explicitly used, the PHP parser will treat it as a filesystem wrapper. The file name parameters passed to the file system functions fopen(), file_get_contents(), etc. use the file:// protocol by default. When the file name does not begin with a forward slash, a backslash, or the driver in Windows
