The include()
and require()
statements allow you to incorporate the code from one PHP file into another PHP file. Including a file achieves the same outcome as copying and pasting the script from the specified file into the calling location.
You can save time and effort by using includes — simply store a block of code in a separate file and include it wherever needed using the include()
and require()
statements, instead of typing the same code repeatedly. A common practice is to include headers, footers, and menus across all pages of a website.
The basic syntax for the include()
and require()
statements is:
include("path/to/filename"); -Or- include "path/to/filename";
require("path/to/filename"); -Or- require "path/to/filename";
Tip: Like the print
and echo
statements, you can omit the parentheses when using the include
and require
statements, as demonstrated above.
The following example demonstrates how to include common header, footer, and menu codes stored in separate 'header.php', 'footer.php', and 'menu.php' files respectively, across all pages of your website. This method allows you to update multiple pages at once by modifying just one file, reducing repetitive work.
<!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>
You might wonder why we need both include()
and require()
statements since they both serve to include files in PHP scripts.
The key difference is that the include()
statement will generate a PHP warning and allow script execution to continue if the included file is not found. In contrast, the require()
statement will result in a fatal error and halt the script execution if the file cannot be included.
<?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 advisable to use the require()
statement when including files like libraries, function definitions, or configuration files that are crucial for your application to function correctly, such as a database configuration file.
If you unintentionally include the same file (such as functions or classes files) multiple times within your code using the include
or require
statements, it can lead to conflicts. To avoid this issue, PHP provides include_once
and require_once
statements. These statements work similarly to include
and require
but with one key difference.
The include_once
and require_once
statements ensure that the specified file is included only once, even if requested to include it again. If the file has already been included previously, it will not be included again. To illustrate this, consider a 'my_functions.php' file with the following example:
<?php
function multiplySelf($var){
$var *= $var; // multiply variable by itself
echo $var;
}
?>
Here's is the following PHP script within which we've included the 'my_functions.php' file.
<?php
// Including file
require "my_functions.php";
// Calling the function
multiplySelf(2); // Output: 4
echo "<br>";
// Including file once again
require "my_functions.php";
// Calling the function
multiplySelf(5); // Doesn't execute
?>
When you execute the script above, an error message might appear, such as: "Fatal error: Cannot redeclare multiplySelf()". This happens when 'my_functions.php' is included more than once. In such cases, the function multiplySelf()
gets defined multiple times, leading PHP to halt the script and show a fatal error. To resolve this issue, let's rewrite the example using require_once
.
<?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
?>
By using require_once
instead of require
, you can observe that the script functions as intended.