Home  >  Article  >  Backend Development  >  The difference between include, require, and suffix plus once

The difference between include, require, and suffix plus once

angryTom
angryTomforward
2019-10-29 17:47:362383browse

The difference between include, require, and suffix plus once

The difference between include, require, and the suffix plus once

include(), require() The statement includes and runs the specified file. The two structures are exactly the same in the include files, the only difference is the error handling. require()When the statement encounters that the included file does not exist or an error occurs, it will stop executing and report an error. include() then continue.

include('hello.php'); 
echo 'include test final!';//include报错,但是会继续执行,显示:include test final! 
require('hello.php'); 
echo 'require test final!';//require报错,停止代码的执行。

1. include(/path/to/filename)

include() statement will include a file at the location where it is called. Including a file has the effect of copying the specified file's data at the location of the statement.

You can ignore the parentheses when using include().

The include() statement can be executed based on conditions. There is a strange phenomenon when using include() in a conditional statement. It must be surrounded by statement block braces or other statement brackets.

2. include_once(filename)

The include_once() function has the same function as include, but it will first verify whether the file has been included. If it is already included, include_once will no longer be executed. Otherwise, the file must be included. It's exactly the same as include except this.

3. require(filename)

require() is largely the same as include. It includes a template file to the location where the require call sits. .

There are two important differences between require and include. First, regardless of the location of require, the specification file will be included in the script where require occurs. For example, even if require is placed in an if statement that evaluates to false, the specified file will still be included.

The second important difference is: when require fails, the script will stop running, while in the case of include, the script will continue to execute.

4. require_once(filename)

As the website grows larger, some files may be included repeatedly. This may not be a problem, but if you modify the variables of the included file, they will be overwritten because the original file is included again later, which may not be desired. Another problem that can arise is conflicting function names in included files. These problems can be solved using require_once.

require_once function ensures that the file is included only once. After encountering require_once, subsequent attempts to include the same file will be ignored.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of The difference between include, require, and suffix plus once. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:duwenfei.com. If there is any infringement, please contact admin@php.cn delete