Split pages into reusable pieces with include, require, and template partials. Frameworks (Blade, Twig) add escaping and layout inheritance—understand plain includes first.
include vs require
require— fatal error if missing (critical files)include— warning if missing (optional partials)require_once/include_once— skip if already loaded
Simple layout pattern
// header.php
<!DOCTYPE html><html><head><title><?= $title ?></title></head><body>
// page.php
$title = 'Products';
require 'header.php';
echo '<main>...</main>';
require 'footer.php';
Escaping in templates
Always escape dynamic HTML: <?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>. Never mix SQL into templates—that belongs in data layer with prepared statements.
Self-check
- When choose
requireoverinclude? - Why separate layout partials?