How to change title of the page after including header.php?

How to change title of the page after including header.php?

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

Stefan Bogdanescu

Founder & Senior Architect

Dynamic Page Titles in PHP: Bypassing Static Includes

You want to set a specific page title after including a header file, but your current setup leaves you stuck with a static value. The code snippet provided demonstrates a common pattern where header.php defines the <title> tag immediately upon inclusion, effectively locking that value before any subsequent logic in page.php can run.

// page.php
include("header.php");
$title = "TITLE"; 
// header.php
<title><?php echo $title; ?></title>

In this specific arrangement, the browser receives the HTML <title> tag as soon as header.php is parsed. Even though you reassign $title later in page.php, that new value never reaches the output buffer for the title element because the tag has already been sent to the client. The variable $title inside header.php was empty or held a default value at the moment of inclusion, and PHP does not maintain a live link to update that specific string later in the script execution flow.

To achieve dynamic titles, you must separate the definition of the title from the output of the HTML head section. The most robust solution is to move the <title> tag generation to a location where it is rendered after all variables have been finalized.

Using a Dedicated Layout Function

The cleanest architectural approach involves creating a layout function or extending a class that handles the header output, rather than using raw include statements for partials. This allows you to control exactly when the title is injected into the document head.

You can modify your main template structure to accept a title parameter and render the <title> tag only after all child templates have had a chance to set their values. For instance, define a function in header.php that returns the HTML string for the header, excluding the <title> tag, and then call this function from a central layout file that sits after your content logic.

// helper/header-functions.php
function renderHeader($title = null) {
    // Render meta tags, styles, scripts, etc.
    echo '<html><head>';
    if ($title) {
        echo '<title>' . htmlspecialchars($title) . '</title>';
    } else {
        echo '<title>Default Title</title>';
    }
    echo '</head><body>';
}

// page.php
renderHeader("My Dynamic Page Title");
// ... rest of the content

This pattern ensures that the title is determined at the point of function call, not at inclusion time. If you are working within a framework like Laravel, this separation of concerns is even more critical for maintaining clean Blade templates where dynamic data often dictates layout elements. You might find similar patterns in how Laravel handles partials versus layouts, ensuring that your view hierarchy respects the order of execution.

The Output Buffer Alternative

If refactoring your include structure is not feasible and you must stick to the current file layout, you can manipulate PHP's output buffering. By starting an output buffer before including header.php, you can capture the initial HTML, modify it after setting the new title, and then flush the buffer.

ob_start();
include("header.php"); // This outputs <title>Old Title</title> to the buffer
$title = "My New Title";
$content = ob_get_clean(); // Get the captured content
// Now inject the new title into $content string before echoing
$content = str_replace('<title>Old Title</title>', '<title>' . $title . '</title>', $content);
echo $content;

While this works, it introduces fragility. You are now relying on string replacement to modify generated HTML, which breaks if your header.php contains multiple <title> tags or if the structure changes slightly. This approach is generally discouraged in production code unless you have no other option. It also makes caching strategies more complex because the output varies based on runtime variables rather than static content.

Best Practices for Dynamic Headers

Regardless of the method chosen, always sanitize user input before inserting it into a title tag. Using htmlspecialchars() is mandatory to prevent Cross-Site Scripting (XSS) vulnerabilities if $title ever contains untrusted data. Furthermore, consider whether every single page truly needs a custom title or if a standard layout component with an optional title parameter is a better fit for your project's scale.

For teams using modern PHP stacks or frameworks, the separation of logic and presentation usually favors defining header content in a dedicated controller or view component that executes after all data gathering is complete. Resources like https://laravelcompany.com often highlight how frameworks handle these state management issues natively, encouraging developers to let the framework manage the lifecycle rather than fighting against include directives.

Our network