What is the difference between include() and require() in PHP?
Both include and require add the code of another file into your script. The difference is what happens when the file is missing.
| Statement | If the file is missing |
|---|---|
| include | Shows a warning and the script continues |
| include_once | Same as include, but does not add the same file again |
| require | Shows a fatal error and the script stops |
| require_once | Same as require, but does not add the same file again |
<?php
echo "Start<br>";
include "missing.php"; // warning, but the script goes on
echo "After include<br>";
require "missing.php"; // fatal error, the script stops here
echo "After require"; // this line never runs
?>
What you will see: “Start”, then a warning, then “After include”, and then a fatal error. The text “After require” is never shown.
Use require for files that your page can not work without, like database connection or config. Use include for optional parts, like a sidebar. Read more in PHP File Inclusion.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |