Splitting Code Across Files: include and require
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
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 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.
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.
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.
| Statement | On a missing file | Loads the file again if already included? |
|---|---|---|
| include | Warning, script continues | Yes |
| require | Fatal error, script stops | Yes |
| include_once | Warning, script continues | No — safely skipped |
| require_once | Fatal error, script stops | No — safely skipped |
Coding Challenges
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 solutionExplain, 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 solutionWrite 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 solutionChapter 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