Includes
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
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
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
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.
| Statement | Missing file behaviour | Already included? |
|---|---|---|
| include | Warning, script continues | Includes again |
| require | Fatal error, script stops | Includes again |
| include_once | Warning, script continues | Skips |
| require_once | Fatal error, script stops | Skips |
A Practical Pattern: Shared Function Library
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.
__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
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.
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.
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.
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