Split a PHP page with include and require

Beginner  Nov 3, 2026

Reuse headers, footers and settings instead of copying them.

Split a PHP page with include and require
The steps of this tutorial at a glance.
The steps of this tutorial at a glance.

The problem

If every page has its own copy of the menu, changing one link means editing every file.

The solution

Put shared parts in their own files:

site/
  includes/header.php
  includes/footer.php
  index.php
  about.php
<?php // index.php
require __DIR__ . '/includes/header.php';
?>
<h1>Home</h1>
<?php require __DIR__ . '/includes/footer.php'; ?>
How the code in this section fits into the page.
How the code in this section fits into the page.

include vs require

  • require stops the page with a fatal error if the file is missing. Use it for things the page cannot work without.
  • include only gives a warning and continues.
  • require_once and include_once load a file only one time. Use them for files that define functions or classes.
The main points of this section.
The main points of this section.

Always use __DIR__

__DIR__ is the folder of the current file. It makes paths work from any page, in any folder.

Never include user input

include $_GET['page'] . '.php';   // DANGEROUS

Use a fixed list of allowed names instead.

Never include user input
Never include user input

More tutorials