Includes

Course 1 · Ch 9
Includes: include, require, and Organising Code Across Files
Splitting a growing script into separate, reusable files — headers, footers, and shared function libraries

Every example so far has lived in one file. Real sites quickly accumulate shared pieces — a header, a footer, a set of helper functions — that are needed on many pages. PHP's include and require pull the content of one file into another at the exact point they're called, avoiding the need to copy-paste the same code everywhere.

include — Pull in Another File's Content

header.php
<?php $siteName = "osztromok.com"; ?> <header> <h1><?= $siteName ?></h1> </header>
index.php
<?php include 'header.php'; ?> <p>Welcome to the homepage.</p> <?php include 'footer.php'; ?>

When index.php runs, PHP inserts the entire contents of header.php right where the include statement is — including any variables it defined, like $siteName above, which then becomes usable in index.php too.

require — Same Idea, But Treated as Essential

<?php require 'config.php'; // if this file is missing, STOP the script entirely with a fatal error include 'sidebar.php'; // if this file is missing, just a warning — the rest of the script still runs ?>
require stops the script on a missing file; include just warns and continues
Use require for anything the rest of the script genuinely cannot function without — a database connection file or core configuration is the classic example. Use include for something genuinely optional, like a sidebar widget, where the page should still mostly work even if that one piece fails to load.

include_once / require_once

<?php require_once 'functions.php'; require_once 'functions.php'; // PHP recognises this was already included, and skips it silently ?>

Without the _once suffix, including the same file twice would attempt to redefine the same functions a second time, causing a fatal "cannot redeclare function" error. require_once/include_once remember which files have already been loaded and skip duplicates — genuinely the safer default whenever a file might end up included indirectly from more than one place.

StatementMissing file behaviourAlready included?
includeWarning, script continuesIncludes again
requireFatal error, script stopsIncludes again
include_onceWarning, script continuesSkips
require_onceFatal error, script stopsSkips

A Practical Pattern: Shared Function Library

functions.php
<?php function formatPrice($amount) { return sprintf("£%.2f", $amount); } ?>
product.php
<?php require_once 'functions.php'; echo formatPrice(9.5); // "£9.50" — formatPrice is now available, defined in functions.php ?>

This is one of the most common real-world uses of require_once — keeping every reusable function (from Chapter 5) in one shared file, then pulling it into any page that needs those functions, rather than redefining them separately everywhere.

Paths matter — relative paths are resolved from the file doing the including, which can be a source of subtle bugs
A common, more robust approach uses __DIR__ (a built-in constant meaning "the folder this current file is in") combined with the path, e.g. require_once __DIR__ . '/functions.php'; — this avoids "file not found" errors that can otherwise occur when the same file is included from scripts living in different folders.

Coding Challenges

Challenge 1

Create two files: greeting.php defining a variable $message = "Hello from the included file!", and main.php which includes it and echoes $message. Confirm in your own words why this works even though $message was never defined directly in main.php.

📄 View solution
Challenge 2

Create math_functions.php with a function square($n) that returns $n * $n. Create a second file that uses require_once to pull it in twice (deliberately, to test the "once" behaviour), and call square(5) to confirm no redeclaration error occurs.

📄 View solution
Challenge 3

Build a minimal header.php and footer.php (each just a few lines of HTML), then a page.php that includes both around some page-specific content in the middle. Use require for one and include for the other, and explain in a comment which you chose for which and why.

📄 View solution

Chapter 9 Quick Reference

  • include 'file.php' — pulls in another file's content; missing file → warning only
  • require 'file.php' — same, but missing file → fatal error, script stops
  • include_once / require_once — skips re-including a file already loaded; safer default for shared function libraries
  • Variables defined in an included file become usable in the including file too
  • __DIR__ . '/file.php' — robust path pattern, avoids relative-path bugs across different calling locations
  • Rule of thumb: require for essentials (config, DB connection), include for optional pieces (sidebar)
  • Next chapter: course capstone — building a small multi-file PHP project bringing everything together