Home  >  Article  >  Backend Development  >  Summary of the differences between PHP include file functions include, include_once, require, and require_once_PHP tutorial

Summary of the differences between PHP include file functions include, include_once, require, and require_once_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:40938browse

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

For example the following code:

The code is as follows: include('hello.php');

echo 'include test final!';//include reports an error, but will continue to execute, displaying: include test final!

require('hello.php');

echo 'require test final!';//require reports an error and stops the execution of the code.

One sentence summary:

1.include() generates a warning

2.require() results in a fatal error

In other words, if you want to stop processing the page when a file is missing, don’t hesitate to use require() . This is not the case with include() and the script will continue to run. Also make sure the appropriate include_path is set.

That is to say, the required file is read when the program is parsed again, instead of after parsing, if the required file cannot be read, the next step cannot be taken. Therefore, if the file is not included correctly and will cause the program to be included, it is better to use require. It may be slightly more efficient.

Note: require() will include the file anyway, while include() can selectively include:

The code is as follows:

if(FALSE){

require('x.php');

}

if(FALSE){

include('s.php');

}

?>

In the above code: x.php will definitely be included, but s.php will definitely not be included.

Two methods provide different usage flexibility:

Require is used 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 the file specified by require and make it a part of the PHP program web page.

include is used like include("MyIncludeFile.php"); . This function is usually placed in the processing section of flow control. The PHP program web page reads the include file when it reads it. In this way, the process of program execution can be simplified.

1. Usage syntax and introduction

1. include()

Syntax: include(/path/to/filename)

The

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.

Parenses can be ignored 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()

Syntax: include_once(filename)

The

include_once() statement includes and runs the specified file during script execution. This behavior is similar to the include() statement. The only difference is that include_once() will first determine whether the file has been included before. If it has been included, this inclusion will be ignored.

include_once() should be used when you want to ensure that it is only included once to avoid problems such as function redefinition and variable reassignment.

Summary: 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()

Syntax: require(filename)

require() is largely the same as include, including 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()

Syntax: require_once(filename)

The

require_once() statement includes and runs the specified file during script execution. This behavior is similar to the require() statement. The only difference is that require_once() will first determine whether the file has been included before. If it has been included, it will ignore this inclusion.

require_once() should be used when you want to ensure that it is only included once to avoid problems such as function redefinition and variable reassignment.

Summary: 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 later included again, 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.

The 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.

2. Summary of differences

1. The difference between include() and require() statements.

The difference between the two: These two structures are exactly the same except how to handle failure.

include() generates a warning and the script will continue running.

require() will cause a fatal error and the script will stop running.

In other words, use require() if you want to stop processing the page when a missing file or error is encountered. If you want to continue processing the page when an error is encountered, use include().

Note that before PHP 4.3.5, syntax errors in include files did not cause the program to stop, but from this version onward they will.

2. The difference between include_once(), require_once() and include(), require()

include_once(), like require_once(), should be used when the same file may be included more than once during script execution, and you want to ensure that it is only included once to avoid problems such as function redefinition and variable reassignment. . This is the main difference between include_once() and require_once() and include() and require().

3. Issues that need attention

1. Path problem

Especially when nested includes, you must pay attention to the path of the included files.

For example, file A contains file B, file B contains file C, and files A, B, and C are not in the same folder. It is often easy to make mistakes at this time.

Solution: You can use the dirname(__FILE__) statement, which means to get the absolute path of the current script. Such as: require_once(dirname(__FILE__).'/config.php');

2. Efficiency issues

include_once(), require_once(), compared with include(), require(), are less efficient, because they must at least first determine whether the file is included. This problem has been greatly improved in the PHP5 version, but there is still a difference in efficiency.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/750257.htmlTechArticleinclude() and require() statements include and run the specified file. The two structures are exactly the same in the include files, the only difference is the error handling. The require() statement does not work when encountering an included file...
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