The difference between JS include and require

differences between include and require introduced in JS

Apr.05,2022

1, loading failures are handled differently
include and require differ in the way they deal with incoming files, the biggest difference is that
include generates a warning when introducing non-save files and the script continues to execute, while
require causes a fatal error and the script stops execution.

<?php
include 'hello.php';
echo 'world';
?>

if hello.php does not exist, the sentence echo 'world' can continue.

require 'hello.php';
echo' world';
? >
if hello.php does not exist, the sentence echo 'hello' will not be executed and will stop at require.

2, include () is a conditional inclusion function, while require () is an unconditional inclusion function.

if(FALSE){
include 'file.php'; //file.php
}
if(FALSE){
require 'file.php'; //file.php

3
includerequire

$retVal = include(' somefile.php' );
if(!empty($retVal)){
echo "";
}else{
echo "";
}
The files that need to be referenced by

include () are read and evaluated every time, and the files that need to be referenced by
require () are processed only once (in fact, the contents of the files that need to be referenced during execution replace the require () statement)

you can see that it is more efficient to use require () if there is code containing one of these instructions and code that may be executed multiple times.
if you read a different file each time the code is executed, or if there is a loop that iterates through a set of files, use include (),

require usually uses the method, and this function is usually placed at the front of the PHP program. Before execution, the PHP program will read in the imported file specified by require, making it part of the PHP program web page. A commonly used function can also be introduced into a web page in this way.

include usually uses a method, which is usually placed in the processing part of the process control. The PHP program page reads the include file only when it is read. In this way, the process of program execution can be simplified

another question about whether to add parentheses after include and require,

in theory: whether include and require are followed by parentheses makes no difference to the execution result, but adding parentheses is less efficient, so it can be followed without parentheses.

Menu