A detailed explanation of the differences between include and require in PHP

  • 2020-10-07 18:36:35
  • OfStack

In PHP, include() functions the same as require(). include (include_once) and require (require_once) both read the included file code to the specified location, but the usage is different: include() is a conditional inclusion function, while require() is an unconditional inclusion function.

1. Different ways of use

(1) The usage of require is as follows: require(" requireFile.php "); . This function is usually placed at the front of the PHP program. Before the PHP program runs, it reads in the file specified by require to make it part of the PHP program web page. A commonly used function can also be introduced into a web page in this way. The introduction is unconditional and occurs before the program executes, importing whether the condition is true or not (it may not).
(2) Use of include such as include(" ES27en.ES28en "); . This function 1 is typically placed in the process section of the process control. The PHP program page reads the include file only when it reaches it. In this way, the process of program execution can be simplified. The introduction is conditional, occurs at program execution time, and is imported only if the condition is true (which simplifies the compiled generated code).

For example, in the following example, if the variable $somgthing is true, the file somefile will be included:


if($something){
include("somefile");
}

But whatever $something is, the following code will include the file somefile in the file:

if($something){
require("somefile");
}

The following interesting example illustrates the difference between these two functions.

$i = 1;
while ($i < 3) {
require("somefile.$i");
$i++;
}

In this code, the program will include the same file every time it loops. This is obviously not the programmer's intention, and we can see from the code that this code wants to include different files in each loop. To do this, you must turn to the function include() :

$i = 1;
while ($i < 3) {
include("somefile.$i");
$i++;
}

2. Error execution is different

The difference between include and require: when include introduces a file, it prompts if it encounters an error and continues to run the code below. When require introduces a file, it prompts if it encounters an error and stops running the code below. Take the following example:


Write two php files named test1.php and ES63en2.php. Note that there should not be one file named test3.php in the same directory.

test1.php


<?PHP
include  ( " test3.php " );
echo   " abc " ;
?>

test2.php

<?PHP
require ( " test3.php " )
echo   " abc " ;
?>

Browse the first file, because test999.php file is not found, we see the error message, at the same time, abc is shown below the error message, you may see the following situation:
Warning: include(test3.php) [function.include]: failed to open stream: No such file or directory in D:\WebSite\test.php on line 2

Warning: include() [function.include]: Failed opening 'test3. C:\php5\pear') in D:\WebSite\test.php on line 2
abc (the following is implemented)

Browse the second file, because test3.php file is not found, we see the error message, but abc is not shown below the error message, you may see the following situation:
Warning: require(test3.php) [function.require]: failed to open stream: No such file or directory in D:\WebSite\test2.php on line 2

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

The following is not executed and ends directly
In short, include is invoked when it is executed, conditionally, while require is a preset behavior, unconditionally.


Related articles: