When building websites, there are often pieces of code that need to be reused across multiple pages, such as headers, footers, navigation menus, or sidebars. Instead of duplicating this code on every page, PHP provides two powerful functions, include()
and require()
, to help you manage and reuse code effectively. This not only makes your code more organized but also easier to maintain.
In this tutorial, we’ll explore how to use include
and require
in PHP, understand their differences, and see how they can improve the structure of your web applications.
What Are PHP include()
and require()
?
Both include()
and require()
are PHP functions that allow you to insert the content of one PHP file into another PHP file before the server executes it. This is useful for reusing components like headers, footers, or even whole sections of code.
Basic Syntax
include('file-to-include.php');
require('file-to-require.php');
- The file paths can be relative or absolute depending on where the files are located.
Example: A Simple Website Structure
Let’s say you’re building a basic website with multiple pages. Instead of repeating the HTML code for the header, footer, or navigation menu on every page, you can split these components into separate PHP files and reuse them across your pages.
Step 1: Create Reusable Components
- header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Website</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>
</nav>
</header>
- footer.php
<footer>
<p>© 2024 My Website</p>
</footer>
</body>
</html>
Step 2: Use include()
in Your Main Pages
- index.php (Home Page)
<?php include('header.php'); ?>
<main>
<h2>Home Page</h2>
<p>This is the home page content.</p>
</main>
<?php include('footer.php'); ?>
- about.php (About Page)
<?php include('header.php'); ?>
<main>
<h2>About Us</h2>
<p>This is the about page content.</p>
</main>
<?php include('footer.php'); ?>
- contact.php (Contact Page)
<?php include('header.php'); ?>
<main>
<h2>Contact Us</h2>
<p>This is the contact page content.</p>
</main>
<?php include('footer.php'); ?>
How It Works:
- The header and footer files are included at the top and bottom of each main page (e.g.,
index.php
,about.php
). - This allows you to reuse the header and footer across multiple pages. If you need to change the header or footer, you only have to modify
header.php
orfooter.php
, and the change will reflect on every page.
The Difference Between include
and require
Both include
and require
do the same thing—insert the content of one PHP file into another—but they handle errors differently:
include()
- If the specified file is not found, PHP will throw a warning but continue executing the rest of the script.Example:
include('nonexistent-file.php'); // Generates a warning but script continues.
echo "This will still be displayed.";
require()
- If the specified file is not found, PHP will throw a fatal error and stop executing the script entirely.Example:
require('nonexistent-file.php'); // Generates a fatal error and stops the script.
echo "This will not be displayed.";
When to Use include
and require
- Use
require
when the file is essential to your application. For example, when including database connection files, critical configuration settings, or necessary class files. - Use
include
when the file is optional or non-critical. For example, you may include a sidebar or widget that, if missing, shouldn’t break the entire site.
Using include_once()
and require_once()
PHP also provides the include_once()
and require_once()
functions, which work similarly but ensure that the file is included only once during the execution of a script. This is particularly useful to avoid redeclaration errors (e.g., when including configuration files or class definitions).
Syntax
include_once('file-to-include.php');
require_once('file-to-require.php');
Example
- config.php (Configuration File)
<?php
// Database connection or site settings
$siteName = "My Awesome Website";
?>
- header.php
<?php require_once('config.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $siteName; ?></title>
</head>
<body>
<header>
<h1><?php echo $siteName; ?></h1>
</header>
require_once()
ensures thatconfig.php
is included only once, even if it’s called multiple times within other files.
Conclusion
Using include()
and require()
in PHP is an excellent way to keep your code organized, avoid duplication, and manage reusable components like headers, footers, or configuration settings. Understanding when to use include
vs. require
is key to handling errors in a way that suits your application’s needs.
Key Takeaways:
- Use
include()
for non-critical files where the application can still function if the file is missing. - Use
require()
for critical files that are necessary for the script to run properly. - Use
include_once()
andrequire_once()
to avoid including files more than once.
Now you can structure your PHP web applications more efficiently by reusing components and ensuring they load as expected!