I know include
and require
is used for inserting php in other files like html files. But what I can undertand is the timing of the use of both. Can anyone explain deeply into the two of them? I find some information online but they're all not well explained I think, I mean not clear enough.

- 29
- 3
-
1You're better off asking specific questions about specific things you had trouble understanding and problems in usage you ran into. – pvg Sep 17 '17 at 09:48
-
If you `require`, execution of the code will stop if it fails to find the required file. If you `include` it doesn't stop. – KIKO Software Sep 17 '17 at 09:48
2 Answers
In the PHP documentation about required they say this:
require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.
and for include the say this
The include statement includes and evaluates the specified file. read more
Explicit example
This line using include is going to include the Php file because the file exists.
<? include("file1.php"); //This file exists ?>
This line using include is going to show a warning in your interface because the file does not exist. Showing up this message on your interface depends on the level of error you have put in your php.ini
<? include("filenotexist.php"); //This file NO exists ?>
Result
Warning: include(filenotexist.php) [function.include]: failed to open stream: No such file or directory in ****** on line 6
Warning: include() [function.include]: Failed opening 'filenotexist.php' for inclusion (include_path='.;C:\php\pear') in **** on line 6
This line using required is going to do the same as the include, it's going to include the php file because the file exists.
<? require("file1.php"); //This file exist ?>
This line using required, so you get a warning and a fatal error because the file does not exists. This fatal error is the main difference you are looking for.
<? require("filenoexists1.php"); //This file NO exist ?>
Result
Warning: require(filenoexists1.php) [function.require]: failed to open stream: No such file or directory in **** on line 6
Fatal error: require() [function.require]: Failed opening required 'filenoexists1.php' (include_path='.;C:\php\pear') in *** on line 6
And there is even another interesting function called required_once. If you required several times the same file
<? require("hola.php"); //This file exist ?>
<? require("hola.php"); //This file exist ?>
<? require("hola.php"); //This file exist ?>
<? require("hola.php"); //This file exist ?>
<? require("hola.php"); //This file exist ?>
The result is 5 times hola. It makes sense because you required the file 6 times.
hola hola hola hola hola
Required_once even if you include it 5 times, Php will only include it once.
<? require_once("hola.php"); //This file exist ?>
<? require_once("hola.php"); //This file exist ?>
<? require_once("hola.php"); //This file exist ?>
<? require_once("hola.php"); //This file exist ?>
<? require_once("hola.php"); //This file exist ?>
the result is
hola
Hope it helps Daniel

- 556
- 6
- 16
-
The poster specifically says they have trouble understanding the documentation so it's not clear what pasting the documentation at them is going to accomplish. – pvg Sep 17 '17 at 10:01
-
@pvg I put the documentation link just in case he doesn't know about it. He is not saying at any moment "I don't understand the documentation". And I even put the explanation about require and include in my answer. He doesn't have to go to the link to understand the difference. In addition, the answer can help more people, not only the poster, so you have to think about it as well. – Daniel Botero Correa Sep 17 '17 at 10:08
-
PHP's include, require and, to complete the list, include_once and require_once, are statements that allow one PHP file to fetch another file (in most cases another PHP file), and insert and evaluate the fetched file at the position of the statement.
These statements are not, as you wrote, for "inserting php in other files like html files", instead they are the concept that allows a PHP application to have its codebase structured into separate files. Code fetching other code fetching other code and so on.
The difference between the mentioned statements is basically:
include 'a/file/that/does/not/exist.php'; //emits a warning, but doesn't stop the script
require 'a/file/that/does/not/exist.php'; //throws a fatal error and stops script execution
... and using require_once and include_once, you can prevent 'double-includes', which could cause problems like function redefinitions

- 1,023
- 11
- 15