Creating a PHP header/footer

Creating a PHP header/footer

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Creating a PHP header/footer

Separating your header and footer logic into reusable files is a fundamental shift in how you structure PHP applications. The goal isn't just to avoid copy-pasting code; it's about managing state and ensuring that every request includes the necessary shared resources—like navigation bars, CSS links, or authentication checks—without bloating every single view file.

The most straightforward approach for a simple site involves using PHP's include or require statements combined with output buffering. This technique allows you to wrap your main content in a block that gets captured and rendered later, effectively injecting the header and footer around the dynamic content.

The Output Buffering Technique

For a "simplistic" implementation where you don't want to deal with complex frameworks or session management immediately, output buffering is often the cleanest path. You start by initializing an output buffer at the very top of your main entry point (e.g., index.php). This captures everything that follows until you explicitly flush the buffer.

Here is a basic pattern using ob_start():

<?php
// index.php or app.php

// Start buffering to capture output
ob_start();

// Include the header file
require 'header.php';

// Your main content goes here
echo "<h1>Welcome to the site</h1>";
echo "This is the main content.";

// Include the footer file
require 'footer.php';

// Flush the buffer to send the captured HTML to the browser
ob_end_flush();

In this setup, header.php and footer.php should contain only the HTML markup. They might look like this:

header.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Site</title>
    <!-- Shared CSS links -->
</head>
<body>
    <header>
        <!-- Navigation and branding -->
    </header>

footer.php

    <footer>
        <!-- Copyright, credits, scripts -->
    </footer>
</body>
</html>

This method works because ob_start() pauses the normal output stream. When you require the header and footer files, their HTML is stored in memory rather than sent to the browser immediately. Once your main content is echoed, ob_end_flush() sends the entire accumulated block at once. This ensures that the header and footer are rendered exactly once per request, regardless of how many times they might be logically "included."

Handling State and Variables

A common pitfall in this simple approach is passing variables between files. If you need to pass a user's name or a specific menu item into header.php, you cannot simply echo it inside the header file without defining a function first.

To maintain separation of concerns, define helper functions in your main entry file that accept necessary variables and inject them where needed:

// index.php

ob_start();

function renderHeader($pageTitle) {
    require 'header.php';
}

function renderFooter() {
    require 'footer.php';
}

renderHeader("Dashboard"); // Passes variable to header scope context if needed
echo "<h1>Content</h1>";
renderFooter();

ob_end_flush();

Alternatively, you can pass an associative array containing shared data into header.php and access those variables directly. This keeps the logic contained within the view files while maintaining a single source of truth for your layout structure.

When to Consider Frameworks

While output buffering solves the immediate problem of dynamic inclusion, it lacks the routing and templating capabilities found in modern frameworks. If your site grows beyond simple HTML injection—requiring database queries, user authentication, or complex routing—you will eventually outgrow manual ob_start/ob_end_flush patterns.

In scenarios where you need a more robust architecture, tools like Laravel offer built-in Blade templates that handle layout separation automatically. With Laravel, you define a base layout with header and footer sections, and your views simply extend this layout. For instance, a view file can extend layouts.app, inheriting the header and footer without manual buffering logic.

For more details on building scalable applications with modern PHP stacks, resources like https://laravelcompany.com provide valuable insights into utilizing frameworks to manage complexity as your project scales. However, for a friend's simple site, sticking to native PHP functions keeps dependencies low and the codebase transparent.

Our network