PHP Include & Require

Reuse files across pages.

✍️ 2 exercises in this lesson

Split a website into reusable files (header, footer, config) and load them with:

  • include "header.php";: shows a warning if the file is missing, then continues.
  • require "config.php";: stops the script with a fatal error if the file is missing.
  • require_once / include_once: load the file only once.

Use require for files the page cannot work without.

Key Takeaways: PHP Include & Require

Mastering PHP Include & Require helps you write clean and reliable PHP code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

PHP Include & Require Code Example

PHP
<?php
// footer.php contains:  <footer>&copy; <?= date("Y") ?> CodesInTech</footer>

require_once __DIR__ . "/config.php";
include __DIR__ . "/header.php";
?>
<h1>Welcome to the home page</h1>
<?php include __DIR__ . "/footer.php"; ?>
Output
[header]
Welcome to the home page
© 2026 CodesInTech
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

PHP Include & Require Practice Exercises

Check what you learned in “PHP Include & Require”.

/ 2 solved
Fill in the blank

Load a file only once and stop with an error if it is missing.

 "config.php";
✓ Correct! Not quite. Try again.
Multiple choice

What happens when an included file is missing with include?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services