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.

StatementIf file is missingScript continues?
includeE_WARNING (notice printed)Yes, execution continues
requireE_ERROR (fatal error)No, script stops immediately
1.1 include — warns and continues
<?php
  include 'no-such-file.php';
  echo 'This line still runs.';
?>
1.2 require — fatal, stops the script
<?php
  require 'no-such-file.php';
  echo 'This line never runs.';
?>
Rule of thumb: use 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
?>
Real example from this exact site: open 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>
Note: This page itself is built exactly this way — scroll up and you’ll find <?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';
?>
Learned the hard way on this site: 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';
?>
Included Header: Hello from 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.

Included Footer: generated at 2026-10-27 18:19:03 by inc-demo-footer.php.
<?php
  include __DIR__ . '/tryit/inc-demo-footer.php';
?>
Refresh this page and watch the “generated at” time in the footer box change — proof that the footer file is executed by the server fresh on every request, not cached or hard-coded into this HTML.

6) Best Practices & Common Mistakes

IssueProblemFix
Using include for critical filesPage silently limps along brokenUse require/require_once for must-have files
Including the same config twice“Cannot redeclare” fatal errorUse include_once/require_once
Hard-coded relative pathsBreaks when included from another folderBuild paths with __DIR__
Including user-supplied filenamesLocal File Inclusion (LFI) vulnerabilityNever pass raw user input into include/require paths