Most pages on a site share the same top and bottom: a logo, a menu, a footer. Instead of repeating them, write them once and include them.
header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= htmlspecialchars($pageTitle ?? "My Site") ?></title>
</head>
<body>
<header>
<nav>
<a href="index.php">Home</a>
<a href="about.php">About</a>
</nav>
</header>
<main>
footer.php
</main>
<footer>
<p>© <?= date("Y") ?> My Site</p>
</footer>
</body>
</html>
A page
<?php
$pageTitle = "About us";
require __DIR__ . "/header.php";
?>
<h1>About us</h1>
<p>We write PHP tutorials.</p>
<?php require __DIR__ . "/footer.php"; ?>
The page sets $pageTitle, loads the header (which prints it in the <title>), shows its own content, and loads the footer. The variable is visible inside the included file because the include happens in the same scope.
Mark the current page in the menu
<?php
$current = basename($_SERVER["SCRIPT_NAME"] ?? "");
?>
<nav>
<a href="index.php" <?= $current === "index.php" ? 'aria-current="page"' : "" ?>>Home</a>
<a href="about.php" <?= $current === "about.php" ? 'aria-current="page"' : "" ?>>About</a>
</nav>
aria-current="page" tells screen readers which link is the current page, and you can style it with CSS: a[aria-current="page"] { font-weight: bold; }.
A layout function
If you prefer one call, wrap the pieces in a function.
<?php
function render(string $title, string $body): string
{
return "<html><head><title>" . htmlspecialchars($title) . "</title></head>"
. "<body>" . $body . "</body></html>";
}
echo render("Home", "<h1>Welcome</h1>");
<html><head><title>Home</title></head><body><h1>Welcome</h1></body></html>
Keep PHP out of the way
In a template file, keep PHP simple: print values, loop and test. Calculations belong in the code above or in functions. Controllers and Views goes further on that idea.
Why this matters in WordPress
get_header() and get_footer(). They load header.php and footer.php from your theme, exactly like the example above.