Explanation of Include and Require:
-
include()
andrequire()
:-
Both of these statements are used to include external files into the current file.
-
include()
will generate a warning if the file cannot be found, but it will continue the script execution. -
require()
will generate a fatal error and halt the script if the file cannot be found. -
Usage:
include("path/to/filename.php"); require("path/to/filename.php");
Example:
<!DOCTYPE html> <html lang="en"> <head> <title>Tutorial Republic</title> </head> <body> <?php include "header.php"; ?> <?php include "menu.php"; ?> <h1>Welcome to Our Website!</h1> <p>Here you will find lots of useful information.</p> <?php include "footer.php"; ?> </body> </html>
-
In the above example, files
header.php
,menu.php
, andfooter.php
are included in the current file.
-
-
Difference Between
include()
andrequire()
:-
include()
: Will generate a PHP warning if the file is not found but will continue executing the rest of the script. -
require()
: Will stop the script execution if the file is not found and generate a fatal error.
Example:
<?php require "my_variables.php"; ?> <?php require "my_functions.php"; ?> <!DOCTYPE html> <html lang="en"> <head> <title><?php displayTitle($home_page); ?></title> </head> <body> <?php include "header.php"; ?> <?php include "menu.php"; ?> <h1>Welcome to Our Website!</h1> <p>Here you will find lots of useful information.</p> <?php include "footer.php"; ?> </body> </html>
Tip: It's recommended to use
require()
for files that are essential for your application, such as configuration files, database connections, or function libraries. -
-
include_once()
andrequire_once()
:-
These functions are similar to
include()
andrequire()
, but they ensure that the file is only included once, even if called multiple times. -
This is especially useful when you have functions or classes that should only be defined once, preventing conflicts or errors when files are included multiple times.
Example:
// my_functions.php <?php function multiplySelf($var){ $var *= $var; // multiply variable by itself echo $var; } ?> // main.php <?php // Including file require_once "my_functions.php"; // Calling the function multiplySelf(2); // Output: 4 echo "<br>"; // Including file once again require_once "my_functions.php"; // Calling the function multiplySelf(5); // Output: 25 ?>
-
The
require_once
function ensures that themy_functions.php
file is only included once, so the function is not redefined.
-
-
Best Practices:
-
Use
require()
for essential files: If the file is required for your script to run (e.g., configuration, database connection), always userequire()
to ensure it is included and executed properly. -
Use
include()
for optional files: If the file is not critical (like a footer or a menu that can be omitted), useinclude()
. -
Use
include_once()
andrequire_once()
to prevent redeclaration: Especially when working with functions, classes, or other reusable code to avoid errors caused by multiple inclusions.
-
This approach allows for more maintainable, readable, and DRY (Don't Repeat Yourself) code, especially when dealing with large applications where certain parts (e.g., headers, footers, configurations) are shared across multiple pages.