Home Backend Development PHP Tutorial php 中include()与require()的对比_php基础

php 中include()与require()的对比_php基础

May 17, 2016 am 09:44 AM
include require

php的require()与include(),在性能方面并无大的不同。

仅有的一些不同在于:

include()执行时文件每次都要进行读取和评估;
require()执行时文件只处理一次(实际上,文件内容替换了require()语句)。
即如果有包含这些指令之一的代码和可能执行多次的代码,则使用require()效率比较高。

另外,如果每次执行代码时要读取不同的文件,或有通过一组文件叠代的循环,就应该使用include(),因为可以给想要包括的文件名设置一个变量,当参数为include()时使用这个变量。

php的require()性能与include()虽然有相似的地方,但是通过自己的学习和查找,还是找到了6点区别如下。

1、
不同之处在于,对include()来说,在include()执行时文件每次都要进行读取和评估;
而对于require()来说,文件只处理一次(实际上,文件内容替换了require()语句)。
这就意味着如果有包含这些指令之一的代码和可能执行多次的代码,则使用require()效率比较高。

2、
require是只执行一次的,不,这么说不恰当。应当说,require是先替代,将指定文件的内容代进来,再运行,所以它不知道你是否设置了FOR循环。而include语句,

是什么时候执行到了,什么把指定文件的内容代进来,继续执行。
所以,如果每次执行代码时想读取不同的文件,或者有通过一组文件叠代的循环,就使用 include(),因为可以给想要包括的文件名设置一个变量,当参数为include()

时使用这个变量。

3、
include在执行时,如果 include 进来的文件发生错误的话,不会立刻停止;而 require 则会立刻终止程序,不再往下执行。

4、include可以用在循环; require不行。   

5、include有返回值,而require没有(可能因为如此require的速度比include快)

ok.php里的代码为 echo "ok!";

$login = include('ok.php');
if(!empty($login)){ echo "文件包含成功";
}else{ echo "文件包含失败"; }

最后返回结果为:ok!文件包含成功

只要ok.php里有语句存在,就会返回成功。

在举一个例子:

1.php里的代码如下:
return array(
'ILOVEYOU'=>1,2,3,4
);
?>

index.php里的代码如下:
$a = array_change_key_case(include '1.php');

print_r($a);
?>

访问index.php的结果如下:
Array ( [iloveyou] => 1 [0] => 2 [1] => 3 [2] => 4 )

6、require的使用方法:这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份。常

用的函数,亦可以这个方法将它引入网页中。
include使用方法:这个函数一般是放在流程控制的处理部分中。PHP 程序网页在读到 include 的文件时,才将它读进来。这种方式,可以把程序执行时的流程简单化

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

vue3+vite: How to solve the error when using require to dynamically import images in src vue3+vite: How to solve the error when using require to dynamically import images in src May 21, 2023 pm 03:16 PM

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

What is the difference between php include and include_once What is the difference between php include and include_once Mar 22, 2023 am 10:38 AM

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?

What are the uses of require? What are the uses of require? Nov 27, 2023 am 10:03 AM

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.

Steps to solve the fatal error in the php header: require(): Failed opening required 'data/tdk.php' (include_path='.;C:\php\pear') Steps to solve the fatal error in the php header: require(): Failed opening required 'data/tdk.php' (include_path='.;C:\php\pear') Nov 27, 2023 pm 12:51 PM

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

Steps to solve fatal error in php header: require(): Failed opening required 'data/tdk.php' Steps to solve fatal error in php header: require(): Failed opening required 'data/tdk.php' Nov 27, 2023 am 10:41 AM

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'".

Detailed explanation of the role and usage of the require keyword in PHP Detailed explanation of the role and usage of the require keyword in PHP Jun 28, 2023 pm 11:31 PM

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

How to include one php.ini file within another php.ini file? How to include one php.ini file within another php.ini file? Sep 02, 2023 pm 03:45 PM

Unable to include .ini file in main php,ini file. In contrast, when compiling PHP, the line --with-config-file-scan-dir=PATH​​​​​&a

PHP file:// PHP file:// Aug 20, 2023 pm 06:13 PM

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

See all articles