Imagine every page on your website repeating the exact same header, footer, and navigation menu code. If you needed to change one link, you would have to edit every single file manually. PHP Include Require solves this exact problem by letting you pull shared code into multiple files automatically.

This is Tutorial 6 of 11 in our PHP Advanced series. You will learn the difference between include and require, practice splitting a real page into header and footer files, and build a shared config file.

What is PHP Include Require?

PHP Include Require refers to four closely related statements — include, include_once, require, and require_once — that pull the content of one PHP file directly into another at the point where they are called. This lets you write shared code once and reuse it across your entire project.

Using include

The include statement inserts the content of another file into the current script. Let’s create a simple header file first.

<?php
// File: header.php
echo "<h1>My Website Header</h1>";
echo "<nav>Home | About | Contact</nav>";
?>

Now, inside a different page, you can pull that header in using include.

<?php
include "header.php";

echo "<p>Welcome to the homepage content.</p>";
?>

Running this second file outputs both the header content and the homepage paragraph, even though the header code physically lives in a completely separate file.

Using require

The require statement works almost identically to include, with one critical difference: if the file cannot be found, require stops the entire script with a fatal error, while include only shows a warning and keeps running.

<?php
require "config.php";

echo "Database host: " . $dbHost;
?>
<?php
// File: config.php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
?>

Since database credentials are essential for the rest of the script to function correctly, PHP Include Require best practice says you should use require here, not include — a missing config file should stop execution immediately, not silently continue with undefined variables.

include vs require: A Direct Comparison

Statement If File Is Missing Best Used For
include Warning, script continues Optional content, like a sidebar widget
require Fatal error, script stops Critical files, like database config

Using include_once and require_once

Sometimes a file gets included multiple times accidentally, causing PHP to redeclare functions or variables, which throws errors. Adding _once to either statement ensures PHP Include Require only loads that specific file one single time per script, no matter how many times it is called.

<?php
include_once "functions.php";
include_once "functions.php";

echo "This file was only actually loaded once.";
?>
<?php
require_once "config.php";
require_once "config.php";

echo $dbHost;
?>

Using require_once for configuration and database connection files is extremely common in real projects, since it prevents duplicate connection attempts if the same file gets required from multiple places.

Building a Reusable Header and Footer

Let’s apply PHP Include Require to a realistic three-file structure: a header, a footer, and a main page that combines both.

<?php
// File: header.php
echo "<header>";
echo "<h1>Tutorials Ocean</h1>";
echo "<nav>Home | PHP | WordPress</nav>";
echo "</header>";
?>
<?php
// File: footer.php
echo "<footer>";
echo "<p>&copy; 2026 Tutorials Ocean. All rights reserved.</p>";
echo "</footer>";
?>
<?php
// File: index.php
require_once "header.php";

echo "<main>";
echo "<p>This is the homepage main content.</p>";
echo "</main>";

require_once "footer.php";
?>

Now every page on the site can require_once the same header.php and footer.php, meaning a single edit to either file instantly updates the header or footer across the entire website — a core benefit of PHP Include Require.

Building a Shared Config File

Config files are one of the most practical real-world uses of PHP Include Require. Instead of repeating database credentials in every script, store them once and require the file wherever needed.

<?php
// File: config.php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbName = "school_db";
?>
<?php
// File: connect.php
require_once "config.php";

$conn = mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected using shared config values.";
?>

This pattern of PHP Include Require will directly prepare you for the next tutorial, where a config file like this one becomes the foundation for every database connection across your project.

Passing Variables into Included Files

Variables defined before an include or require statement remain accessible inside the included file, since PHP treats the included code as if it were physically pasted at that exact location.

<?php
$pageTitle = "About Us";
include "page-header.php";
?>
<?php
// File: page-header.php
echo "<title>" . $pageTitle . "</title>";
?>

Here, page-header.php successfully uses $pageTitle even though the variable was defined in a completely different file, demonstrating how PHP Include Require shares scope between the calling script and the included one. For complete official documentation, refer to the official PHP include manual.

Common Mistakes to Avoid

A frequent PHP Include Require mistake is using include for critical files like database configs, allowing the script to continue running with undefined variables instead of stopping safely. Another common issue is incorrect file paths — always double-check whether you need a relative or absolute path based on your folder structure.

What’s Next?

You now understand PHP Include Require — the difference between include and require, the _once variants, and practical patterns like shared headers, footers, and config files. In the next tutorial, we will connect to MySQL using MySQLi Procedural style, building directly on the config file pattern you just learned here.

If you missed the previous lesson, check out Tutorial 5: PHP File Handling Guide, or explore all lessons in our PHP category.

Practice Exercise

Complete the following tasks to reinforce what you learned in this tutorial. Write and run every example yourself — do not just read the code.

  • Create three files: header.php, footer.php, and main.php
  • In header.php, echo an h1 tag with your site name and a nav menu with three links
  • In footer.php, echo a paragraph with a copyright message including the current year
  • In main.php, use require_once to load header.php, echo two paragraphs of homepage content, then use require_once to load footer.php
  • Create a config.php file storing four variables: dbHost, dbUser, dbPass, dbName
  • Create a second file that uses require_once to load config.php, then echoes all four variable values to confirm they transferred correctly
  • Deliberately misspell a filename in an include statement, run the script, and observe the warning — then do the same with require and observe the fatal error, comparing the two behaviors directly

Bonus Challenge: Create a file called navbar.php that uses a variable named $currentPage passed from the calling file to highlight the active menu item differently (for example, wrapping the matching link in a <strong> tag). Include it from two different pages, each setting $currentPage to a different value, and confirm the correct link highlights on each page.

About Author

Arshad Sultan

Arshad Sultan is the Founder of Tutorials Ocean and a Technical Architect & Full-Stack Developer with 15+ years of experience in PHP, Laravel, CRM development, API integrations, and AI-powered automation. He also runs Rapid Code, a software and AI automation agency, and has trained hundreds of students across Karachi's IT institutes since 2010.

Leave a Reply

Your email address will not be published. Required fields are marked *