Home > Article > Backend Development > php include require difference
1. The difference between include and require
In addition to the different ways of processing imported files, include and require also The biggest difference is that include generates a warning when introducing a non-existing file and the script will continue to execute, while require will cause a fatal error and the script will stop executing.
<?php include 'no.php'; echo '123'; ?>
If the no.php file does not exist, the echo '123' sentence can continue to be executed.
include() has the same function as require(), but there are some differences in usage. include() is a conditional inclusion function, while require() is an unconditional inclusion function.
For example, in the following example, if the variable $somgthing is true, the file somefile will be included:
if($something){include("somefile"); }
But no matter what value $something takes, the following code will The file somefile is included in the file:
if($something){require("somefile"); }
2. The difference between include and include_once (the difference between require and require_once)
##include_once (require_once) statement includes and runs the specified file during script execution. This behavior is similar to the include (require) statement, except that if the code in the file is already included, it will not be included again, but only once. include_once (require_once) needs to query the loaded file list, confirm whether it exists, and then load it again.<?phprequire '1.php';require '1.php';?>In this case 1.php is included twice.
<?phprequire '1.php';require_once '1.php';?>In this case, the second inclusion will not be included. Recommended tutorial:
The above is the detailed content of php include require difference. For more information, please follow other related articles on the PHP Chinese website!