SoFunction
Updated on 2025-04-09

Comparison between include and require in php

php's require() and include() are no big differences in performance.

The only differences are:

When executing include(), the file must be read and evaluated every time;
The file is processed only once when required() is executed (in fact, the file content replaces the require() statement).
That is, if there is code that contains one of these instructions and code that may be executed multiple times, using require() is more efficient.

In addition, if you want to read a different file every time you execute the code, or there is a loop through a set of files iterations, you should use include() because you can set a variable to the file name you want to include, and use this variable when the parameter is include().

Although the performance of php requires() is similar to include(), 6 differences are found through my own learning and search.

1、
The difference is that for include(), the file must be read and evaluated every time when include() is executed;
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 contains one of these instructions and code that may be executed multiple times, using require() is more efficient.

2、
Require is only executed once, no, it is inappropriate to say this. It should be said that require is replaced first, and then run the contents of the specified file, so it does not know whether you have set up the FOR loop. And include statement,

When did it be executed, what should I put the contents of the specified file in and continue to execute.
So, if you want to read a different file every time you execute the code, or there is a loop through a set of files that are iterated, use include(), because you can set a variable to the file name you want to include, when the parameter is include()

Use this variable when using it.

3、
When executing include, if an error occurs in the file in which include comes in, it will not be stopped immediately; while require will immediately terminate the program and will not be executed further.

4. include can be used in loops; require cannot.

5. include has a return value, but require does not (maybe because of this requirement, the speed is faster than include)

The code in it is echo "ok!";

$login = include('');
if(!empty($login)){ echo "File contains successfully";
}else{ echo "File Inclusion Failed"; }

The final result is: OK! The file contains successfully

As long as there is a statement in it, success will be returned.

In order to give an example:

The code in it is as follows:
<?php
return array(
'ILOVEYOU'=>1,2,3,4
);
?>

The code in it is as follows:
<?php
$a = array_change_key_case(include '');

print_r($a);
?>

The results of the visit are as follows:
Array ( [iloveyou] => 1 [0] => 2 [1] => 3 [2] => 4 )

6. How to use require: This function is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read the file introduced by require, making it a part of the PHP program web page. often

This method can also be used to introduce it into a web page.
include usage method: This function is generally placed in the processing part of process control. The PHP program web page only reads the include file when it is read. This way, the process of execution of the program can be simplified