Home  >  Article  >  Backend Development  >  php include require difference

php include require difference

王林
王林Original
2019-10-09 17:41:343059browse

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 &#39;no.php&#39;;  
echo &#39;123&#39;;
?>

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 &#39;1.php&#39;;require &#39;1.php&#39;;?>

In this case 1.php is included twice.

<?phprequire &#39;1.php&#39;;require_once &#39;1.php&#39;;?>

In this case, the second inclusion will not be included.

Recommended tutorial:

PHP video 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn