Splitting Code Across Files: include and require

Course 1 · Ch 9
Splitting Code Across Files: include and require
Reusing shared PHP code — headers, footers, and helper functions — across many pages without copy-pasting

Every script so far has lived in a single file. Real projects share the same header, footer, or set of helper functions across dozens of pages — copy-pasting that code into every file would mean editing every single one whenever something needs to change. PHP solves this with four statements that pull another file's own code directly into the current script: include, require, include_once, and require_once.

include and require

// header.php <?php echo "<header>My Website</header>"; ?>
// index.php <?php include 'header.php'; echo "<main>Welcome!</main>"; ?>

When index.php runs, include 'header.php' pulls in and executes header.php's own code right at that point in the script, exactly as if its contents had been pasted there directly. Any variables already defined before the include line are visible to the included file, and any variables the included file defines become visible afterward too — they share the same scope.

include vs require — the difference is what happens when the file is missing
If the target file doesn't exist, include emits a warning and the script keeps running — the rest of the page still renders, just missing that piece. require emits a fatal error and stops the script immediately. Use require for anything the page genuinely cannot function without (a database config, a core class definition); use include for something optional, like a sidebar widget that's fine to simply be absent.

include_once and require_once

If two different included files both try to include a third shared file — say, a function library both of them rely on — a plain include would load and execute that shared file's code twice, which for a function definition causes a fatal "cannot redeclare function" error.

// functions.php <?php function greet($name) { return "Hello, " . $name . "!"; } ?>
// index.php <?php require_once 'functions.php'; require_once 'functions.php'; // safely skipped — already loaded echo greet('Sam'); ?>

include_once and require_once track which files have already been pulled in during the current script run, and silently skip any file that's already been loaded — combining "load this file" with "but never load it twice." For a function or class library that might legitimately be included from several different places, require_once is the standard, safe default.

A practical shared-header pattern
A common real-world layout splits a page into header.php (opening HTML, navigation), the page's own unique content, and footer.php (closing HTML, shared scripts) — with every page on the site including the same two shared files at the top and bottom. Updating the navigation menu then means editing header.php once, not every page individually.
StatementOn a missing fileLoads the file again if already included?
includeWarning, script continuesYes
requireFatal error, script stopsYes
include_onceWarning, script continuesNo — safely skipped
require_onceFatal error, script stopsNo — safely skipped

Coding Challenges

Challenge 1

Describe (in comments, since this is a single-file exercise) a two-file setup: greeting.php defines a function sayHello($name), and main.php uses require_once to load it and calls sayHello() with your own name. Write out both files' full code as comments to show the structure.

📄 View solution
Challenge 2

Explain, in your own words as a comment, what would happen if index.php used require 'config.php' but config.php didn't exist, versus what would happen if it used include 'config.php' instead. Write a short code example demonstrating the require case.

📄 View solution
Challenge 3

Write out (as comments describing a 3-file structure) why using require_once instead of require would prevent a "cannot redeclare function" fatal error if both header.php and sidebar.php each try to require the same functions.php file from within index.php.

📄 View solution

Chapter 9 Quick Reference

  • include — pulls in a file; warning + continues if missing
  • require — pulls in a file; fatal error + stops if missing
  • include_once / require_once — same as above, but skips a file already loaded once
  • An included file shares scope with the file that included it — variables flow both ways
  • Use require_once for anything the page can't function without, especially shared function/class libraries
  • Use include for genuinely optional pieces, like an optional sidebar widget
  • Next: the Capstone — combining variables, functions, arrays, strings, and includes into one multi-file project