Kodokon kodokon.com

Organizing your code: include, structure, constants

Split your site into reusable files with include and require, a clear project structure, and centralized constants.

7 min · 3 questions

Open this lesson in Kodokon

As soon as your site grows beyond two pages, duplicating the header, footer, and functions in every file becomes unmanageable. PHP provides four inclusion statements: include, require, and their include_once / require_once variants. They all insert the contents of a file at the point of the call; the difference lies in error handling and repeated inclusions. The _once variants keep track of already-loaded files and include them only once - essential for function files, which cannot be redeclared.

PHP
<?php

declare(strict_types=1);

function e(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES);
}

function formatPrice(float $amount): string
{
    return number_format($amount, 2, '.', ',') . ' €';
}
src/functions.php: helpers reusable across the whole site.

A clear project structure is worth a thousand comments. A common convention: public/ holds the pages served to the browser (it's the only exposed folder), src/ gathers configuration and functions, templates/ contains page fragments (header.php, footer.php). PHP's built-in development server is launched by pointing it at public/.

BASH
mkdir -p my-site/public my-site/src
mkdir -p my-site/templates
cd my-site
php -S localhost:8000 -t public
Creating the structure, then launching the development server.

The project's fixed values (site name, paths, settings) deserve constants rather than variables: they can't be overwritten by accident. const is resolved at compile time, define() at runtime; in practice, prefer const in your configuration files. For paths, always prefix with __DIR__, the folder of the current file: your inclusions will work no matter where the script is launched from.

PHP
<?php

declare(strict_types=1);

const SITE_NAME = 'La Bonne Pizza';
const ITEMS_PER_PAGE = 12;
const UPLOAD_DIR = __DIR__ . '/../public/uploads';

define('BASE_URL', 'http://localhost:8000');
src/config.php: all the configuration in one place.

Each page then becomes a short assembly: load the configuration and functions with require, then include the templates around the specific content.

HTML
<?php
require __DIR__ . '/../src/config.php';
require_once __DIR__ . '/../src/functions.php';

$pageTitle = SITE_NAME . ' - Home';
include __DIR__ . '/../templates/header.php';
?>
<main>
  <h1><?= e($pageTitle) ?></h1>
  <p>Pizza of the day: <?= formatPrice(9.5) ?></p>
</main>
<?php include __DIR__ . '/../templates/footer.php'; ?>
public/index.php: the page is now just an assembly.

Knowledge check

Make sure you remember the key points of this lesson.

  1. What is the difference between include and require?
    • include is faster than require
    • require only works with absolute paths
    • require stops the script with a fatal error if the file is missing, include emits a warning and continues
    • None, they are two aliases
  2. What is require_once for?
    • To guarantee a file is included only once, even if several files request it
    • To include a file only in production
    • To include a file without executing its PHP code
  3. Why prefix inclusion paths with __DIR__?
    • It's mandatory since PHP 8
    • The path becomes relative to the current file and no longer depends on the folder the script is launched from
    • It enables a cache of included files