Home  >  Article  >  Backend Development  >  The difference between include files and require in PHP (with detailed explanation)

The difference between include files and require in PHP (with detailed explanation)

烟雨青岚
烟雨青岚forward
2020-06-08 14:43:312524browse

The difference between include files and require in PHP (with detailed explanation)

The difference between include file include and require in php (with detailed explanation)

1 Using syntax And introduction

1. include() syntax:

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(). When using include() in a conditional statement, it must be enclosed in statement block curly braces or surrounded by other statement wrappers.

2. include_once() syntax:

include_once(filename)

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, it will ignore this inclusion.

3. require() syntax:

require(filename)

require() is largely the same as include, both include a template file into require Call sitting position. There are two important differences between require and include.

1. Regardless of the location of require, the specified file will be included in the script where require appears. For example, even if require is placed in an if statement that evaluates to false, the specified file will still be included.

2. When a require error occurs, the script will stop running, but if include is used, the script will continue to execute.

4. require_once() syntax:

require_once(filename)

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.

2 Summary of differences

1. Difference between include() and require() statements.

Example 1

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

Summary:

⑴ include() generates a warning and the script will continue to run.

⑵ 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 would not cause the program to stop, but they will from this version onwards.

2. require() will include files anyway, while include() can selectively include

Example 2

<?php
 if(FALSE){
   require(&#39;x.php&#39;);
 }
 if(FALSE){
   include(&#39;s.php&#39;);
 }
?>

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

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

include_once() is the same as require_once() and should be used in When the same file may be included more than once during script execution, 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().

Three issues that need attention

1. Path issues

especially nested inclusions When doing so, be sure to pay attention to the path of the included file. 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. For example: require_once(dirname(__FILE__).'/config.php');

2. Efficiency issues

##include_once(), require_once(), Compared with include() and require(), it is 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.

Thank you all for reading, I hope you will make some progress after reading.

Recommended tutorial: "

PHP Tutorial"

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

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