File Inclusion - include() and required() in PHP
It is nothing but calling a file into another file. Whenever you have a requirement to called same piece of code which is available inside another page into required file than we have to go with include mechanism.
Types of File Inclusion
There are four types of functions available to achieve include mechanisms.
- include()
- include_once()
- required()
- required_once()
include()
The include() function takes all the text in a specified file and copies it into the file that uses the include function.
If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
Syntax:
include 'filename';Assume we have a standard footer file called "footer.php", that looks like this.
<?php
echo "<p>Copyright © 2022-" . date("Y") . " Algorithroom.com</p>";
?>
To include the footer file in a page, use the include statement example.
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>Output:
Welcome to my home page!
Some text.
Some more text.
Copyright © 2022-2018 W3Schools.comrequire()
The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.
So there is no difference in require() and include() except they handle error conditions.
It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.
<html>
<body>
<?php include("footer.php"); ?>
<p>This is an example to show how to include wrong PHP
file</p>
</body>
</html>It will produce the result −This is an example to show how to include wrong PHP file. Now lets try same example with require() function.
<html>
<body>
<?php require("footer.php"); ?>
<p>This is an example to show how to include wrong PHP
file</p>
</body>
</html>This time file execution halts and nothing is displayed.
include_once() and require_once()
The include_once() and require_once() statements will only include the file once even if asked to include it a second time i.e.
if the specified file has already been included in a previous statement, the file is not included again.
PHP include() vs require()
The require statement is also used to include a file into the PHP code. However, there is one big difference between include and require. when a file is included with the include statement and PHP cannot find it, the script will continue to execute.
| Aspect | include() | require() |
|---|---|---|
| Error Handling | Issues a warning if the file is missing. | Issues a fatal error if the file is missing. |
| Script Execution | Continues execution even if the file is missing. | Stops execution if the file is missing. |
| Use Case | Optional inclusions. | Mandatory inclusions. |
| Performance | Slightly slower as it is used dynamically. | Slightly faster for static inclusions. |
Best Practices
Use require() for Essential Files
For critical files (e.g., database connections, configurations), use require() to ensure the script does not proceed without them.
Use include() for Optional Files
For non-critical files (e.g., optional templates or extra features), use include().
Use *_once() Variants to Avoid Redundancy
When there is a chance of including the same file multiple times, use include_once() or require_once().
Error Handling
For include(), ensure you handle warnings gracefully, especially in production environments.
While include() and require() serve similar purposes, their behavior in handling errors and script execution differs significantly.
Understanding these differences allows developers to make informed decisions based on the importance of the included file. Use require() for critical dependencies and include() for optional or dynamic content.