PHP Include and Require
As pages grow, you don’t want to copy-paste the same header, footer, sidebar, or database connection code into every file. PHP lets you pull code from another file into the current one using include, require, include_once, and require_once. This page explains the differences and shows two real files being included live right here on the page.
1) include vs require
Both statements pull the contents of one PHP file into another at the point they are called. The difference is what happens when the target file is missing.
| Statement | If file is missing | Script continues? |
|---|---|---|
include | E_WARNING (notice printed) | Yes, execution continues |
require | E_ERROR (fatal error) | No, script stops immediately |
<?php
include 'no-such-file.php';
echo 'This line still runs.';
?>
<?php
require 'no-such-file.php';
echo 'This line never runs.';
?>
require for files your page cannot work without
(a database connection, a core config file). Use include for optional pieces
(a promo banner, an optional widget) where a missing file shouldn’t break the whole page.2) include_once and require_once
If the same file gets included twice (for example, two different includes both need a config file),
PHP will happily run it twice — which can cause “function already declared” or “class already
declared” fatal errors. The _once variants track which files have already been loaded
and skip them the second time.
<?php
include_once 'config.php';
include_once 'config.php'// safe, loaded only once
?>
connection.php at the project root —
it is included with require_once from other scripts so the database connection code
only ever runs a single time per request. track-visitor.php (included near the top
of this very page, in the <head>) is loaded with include_once too, so
visiting-tracking logic can never accidentally double-fire even if a page includes it more than once.
3) Typical Use Case: Shared Header/Footer Files
The most common reason to use includes is to avoid repeating the same markup on every page — navigation bars, sidebars, footers. You are looking at a live example of this pattern right now: every tutorial page on this site, including this one, is built from small reusable pieces.
<?php include_once('php-header.php'); ?>
<div class="wrapper">
<?php include_once('php-sidebar.php'); ?>
<!-- page content goes here -->
</div>
<?php include_once('php-header.php');?> and
<?php include_once('php-sidebar.php');?> near the top of the file, right
before the .wrapper div.4) Path Resolution Gotchas
A relative path like 'includes/config.php' is resolved relative to the
currently executing script, not necessarily the file that wrote the include
line. If file A includes file B, and file B includes a relative path, PHP still resolves it
based on the entry script’s working directory — which can silently break when the same file
is included from pages that live in different folders.
<?php
// Fragile: depends on which script is currently running
include 'connection.php';
// Robust: always resolved relative to THIS file's own folder
include __DIR__ . '/connection.php';
?>
track-visitor.php used to
build its include path in a way that only worked when called from certain folders. Switching it to
resolve the path with __DIR__ made the include reliable no matter which page includes
it. If a shared file behaves differently depending on which page called it, an inconsistent path is
usually the first thing worth checking.5) Live Demo: Real Includes Running on This Page
The two boxes below are not a screenshot or a copy-pasted example — they are produced by two
tiny separate files, tryit/inc-demo-header.php and tryit/inc-demo-footer.php,
pulled into this exact page right now using real PHP include statements:
<?php
include __DIR__ . '/tryit/inc-demo-header.php';
?>
inc-demo-header.php — this text was pulled in with a real PHP include statement.This paragraph belongs to php-includes.php itself, sitting between the two included files.
inc-demo-footer.php.<?php
include __DIR__ . '/tryit/inc-demo-footer.php';
?>
6) Best Practices & Common Mistakes
| Issue | Problem | Fix |
|---|---|---|
Using include for critical files | Page silently limps along broken | Use require/require_once for must-have files |
| Including the same config twice | “Cannot redeclare” fatal error | Use include_once/require_once |
| Hard-coded relative paths | Breaks when included from another folder | Build paths with __DIR__ |
| Including user-supplied filenames | Local File Inclusion (LFI) vulnerability | Never pass raw user input into include/require paths |