1. Overview

The performance of require() is similar to include(), as both include and run the specified file. The difference is that for include(), the file is read and evaluated each time it is executed; whereas for require(), the file is processed only once (in fact, the file content replaces the require() statement). This means that if there is code that may be executed multiple times, using require() is more efficient. On the other hand, if you need to read different files each time the code is executed, or have a loop iterating through a group of files, use include().

The usage of require() is as follows:

require("myfile.php")

This statement is usually placed at the very beginning of a PHP script. Before the PHP program executes, it first reads in the file introduced by the require() statement, making it part of the PHP script file.

The usage of include() is the same as require, for example:

include("myfile.php")

This statement is generally placed in the processing section of flow control.

The PHP script file reads in the included file only when it encounters the include() statement. This approach can simplify the flow of program execution.

  • incluceLoad when needed
  • requireLoad at the beginning
  • _onceThe suffix indicates that already-loaded files are not loaded again

The PHP system has a pseudo-compilation process when loading PHP programs, which can speed up program execution. However, files included with include are still interpreted and executed. If an error occurs in an included file, the main program continues to run; if an error occurs in a required file, the main program stops. Therefore, if an error in the included file has little impact on the system (such as interface files), use include; otherwise, use require.

require() and include() statements are language constructs, not real functions. Like other language constructs in PHP, for example echo() can use the echo("ab") form or the echo "abc" form to output the string abc. The require() and include() statements can also take parameters directly without parentheses.

The include_once() and require_once() statements also include and run the specified file during script execution. This behavior is similar to include() and require(), and the usage is the same. The only difference is that if the code in the file has already been included, it will not be included again. These two statements should be used when, during script execution, the same file may be included more than once, to ensure it is included only once and avoid problems such as function redefinition and variable reassignment.


2. Detailed Explanation

2.1 Error Reporting

includeWhen including a file, if an error is encountered, a prompt is given and the code below continues to run.

requireWhen including a file, if an error is encountered, a prompt is given and the code below stops running.

Let's use an example. Write two PHP files named test-include.php and test-require.php. Note that in the same directory, there should be no file named test-nothing.php.

test-include.php

<?php include 'test-nothing.php'; echo 'abc'; ?>

test-require.php

<?php require 'test-nothing.php'; echo 'abc'; ?>

Browsehttp://localhost/test-include.php, because the file test-nothing.php was not found, we saw an error message, and simultaneously, abc was displayed below the error message. What you see may be similar to the following:

Warning: include(test-nothing.php) [function.include]: failed to open stream: No such file or directory in D:\www\test-include.php on line 2

Warning: include() [function.include]: Failed opening 'test-nothing.php' for inclusion (include_path='.;C:\php5\pear') in D:\www\test-include.php on line 2

abc

Browse http://localhost/test-require.php. Because the file test-nothing.php was not found, we saw an error message, but abc was not displayed below the error message. What you see may be similar to the following:

Warning: require(test-nothing.php) [function.require]: failed to open stream: No such file or directory in D:\www\test-require.php on line 2

Fatal error: require() [function.require]: Failed opening required 'test-nothing' (include_path='.;C:\php5\pear') in D:\www\test-require.php on line 2 

2.2 File Inclusion Methods

When include() executes, the referenced file must be read and evaluated each time; when require() executes, the referenced file is processed only once (in fact, during execution, the content of the referenced file replaces the require() statement). It can be seen that if you have code that contains one of these directives and may be executed multiple times, using require() is more efficient. If you need to read a different file each time the code is executed, or have a loop iterating through a group of files, use include(). You can set a variable for the filename you want to include and use that variable when the parameter is include().

Original link: https://blog.csdn.net/shenpengchao/article/details/52326233