Home >Backend Development >PHP Tutorial >Detailed explanation of the difference between include() and require() in PHP

Detailed explanation of the difference between include() and require() in PHP

墨辰丷
墨辰丷Original
2018-05-25 14:40:391246browse

This article mainly introduces the difference between include() and require() in PHP. It has a very good reference value. Let’s take a look at it with the editor below.

require is used such as require("MyRequireFile.php"); . This function is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.

include Use methods such as include("MyIncludeFile.php");. This function is generally placed in the processing part of flow control. The PHP program webpage only reads the include file when it reads it. In this way, the process of program execution can be simplified.

The two of them have exactly the same purpose, and it does not necessarily have to be which one is placed at the front and which one is placed in the middle. The most fundamental difference between them is the way they handle errors.

require If there is an error in a file, the program will interrupt execution and display a fatal error

include If there is an error in a file, the program will not end but will continue to execute. and displays a warning error.

The following are supplements:

1. include has a return value, but require does not.

2. include() includes and runs the specified file. When the processing fails, include() generates a warning. The imported program code will be executed, and these programs will be executed when executed. Have the same variable scope as the location in the source file where the include() statement was called. You can import static pages from the same server.

3. The function of include_once() is almost the same as include()

The only difference is that include_once() will first check the file to be imported Has it been imported elsewhere in the program? If so, it will not be imported again. (This function is sometimes very important. For example, if the object to be imported declares something you have defined yourself, function, then if this file is imported repeatedly in the same program, an error message will occur during the second import, because PHP does not allow functions with the same name to be declared repeatedly).

4. require() will read the contents of the target file and replace itself with the read contents. If the processing fails, require() will cause a fatal error.

This reading and substitution action occurs when the PHP engine compiles your program code, not when the PHP engine starts executing the compiled program code (PHP 3.0 engine The way it works is to compile and execute one line at a time, but this has changed since PHP 4.0. PHP 4.0 first compiles the entire program code, and then executes the compiled program code at once. During the compilation process No program code will be executed). require() is usually used to import static content, while include() is suitable for importing dynamic program code.

5. Like include_once(), require_once() will first check whether the content of the target file has been imported before. If so, the same content will not be imported again.

6. require is an unconditional inclusion, that is, if require is added to a process, require will be executed first regardless of whether the condition is true or not.

7. require is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. a part. Commonly used functions can also be introduced into web pages in this way.

8. Include is generally placed in the processing part of the process control. The PHP program webpage only reads the included file when it reads it. This method can simplify the process of program execution.

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

Detailed examples of the difference between include and require in PHP

Detailed explanation of require, include, and use distinction in php

Explanation of PHP Include files

The above is the detailed content of Detailed explanation of the difference between include() and require() in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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