Include & Require
Real applications are rarely written as one giant PHP file. As soon as a project grows past a page or two, you want to split logic into separate files — a database connection here, a set of helper functions there, a shared header and footer somewhere else — and then pull them into whichever script needs them. PHP gives you four keywords for that: include, require, include_once, and require_once. They all do the same fundamental thing (pull another file's code into the current script and execute it in place), but they differ in how strictly they react when the file cannot be found.
The basic idea: pulling one file into another
Both include and require take a file path and, at the point where the statement appears, act as if the entire contents of that file were pasted in and executed right there. Any variables already defined in the calling script are visible to the included file, and any variables the included file defines become visible back in the calling script after the include line.
greeting.php
<?php
$greeting = "Welcome back";
echo "{$greeting}, {$username}!";index.php
<?php $username = "Priya"; include "greeting.php";
Welcome back, Priya!
Notice that $username was defined in index.php before the include line, and greeting.php could still read it. This shared scope is convenient, but it also means an included file can silently depend on variables it never declares itself — a common source of confusion in older, loosely organized codebases.
include vs require: what happens when the file is missing
The difference between include and require only shows up when the target file cannot be found (or cannot be read). include emits an E_WARNING, prints a warning message, and then lets the script keep running with whatever code comes after it. require emits a fatal E_COMPILE_ERROR and stops the script immediately — nothing after that line executes.
Missing file with include (script keeps going)
<?php include "does-not-exist.php"; echo "This line still runs.";
Warning: include(does-not-exist.php): Failed to open stream... This line still runs.
Missing file with require (script halts)
<?php require "does-not-exist.php"; echo "This line never runs.";
Fatal error: Uncaught Error: Failed opening required 'does-not-exist.php'
The practical rule of thumb is: use require for anything the script genuinely cannot function without — a database configuration file, a core class definition, the framework bootstrap file. Use include for something optional, like a sidebar widget or an analytics snippet that would be nice to have but shouldn't crash the whole page if it's missing.
include_once and require_once: avoiding double inclusion
If two different files in your project both include the same helper file, and a third file includes both of those, that helper file ends up being pulled in twice. If the helper file declares a function or a class, PHP throws a fatal error the second time around, because you cannot declare the same function or class name twice in one script.
functions.php
<?php
function formatPrice($amount) {
return "$" . number_format($amount, 2);
}Double inclusion causes a fatal error
<?php include "functions.php"; include "functions.php"; echo formatPrice(19.5);
Fatal error: Cannot redeclare formatPrice() (previously declared in functions.php:2)
include_once and require_once solve this by keeping track of every file path PHP has already pulled in during the current request. If the same path is included again, the statement is simply skipped the second time — no error, no re-execution, no redeclaration problem.
Safe repeated inclusion
<?php include_once "functions.php"; include_once "functions.php"; echo formatPrice(19.5);
$19.50
A practical pattern: config, header, and footer files
A very common structure in small-to-medium PHP projects (and one you will still see in plenty of production code) is to split shared pieces into their own files and require them wherever needed.
config.php— database credentials, API keys, environment-specific settings, loaded withrequire_oncebecause nothing works without it.header.php— the opening HTML, navigation bar, and<head>tag, included at the top of every page.footer.php— closing markup, shared scripts, and copyright notice, included at the bottom of every page.functions.php— reusable helper functions shared across many pages, loaded with*_onceto guard against redeclaration.
A typical page assembled from shared pieces
<?php require_once "config.php"; require_once "functions.php"; require "header.php"; echo "<main>Page content goes here.</main>"; require "footer.php";
This pattern keeps each page's own file short and focused on its unique content, while the boilerplate that's identical across every page lives in exactly one place. If the navigation bar needs a new link, you edit header.php once instead of hunting through dozens of pages.
Why manual class includes have mostly disappeared
In older PHP codebases, it was common to see a long block of require_once statements at the top of a file, one for every class the script depended on — require_once "models/User.php";, require_once "models/Order.php";, and so on. As a project grows to hundreds of classes, maintaining that list by hand becomes tedious and error-prone: forget one require_once, and you get a fatal "class not found" error.
Modern PHP projects solve this with Composer's autoloader instead. Composer generates a single autoload file that maps class names to file paths automatically, following a naming convention (PSR-4), so you just write new User() anywhere in your code and PHP loads the right file on demand — no manual require list to maintain. That mechanism deserves its own dedicated walkthrough elsewhere; the point to take away here is that include/require are still exactly the right tool for config files, templates, and small scripts, even though class loading in larger applications has moved on to autoloading.