Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 7 of 9

include, require & Code Organisation

include and require split code across multiple files - the foundation of code organisation before Composer autoloading. require_once and include_once prevent duplicate declarations. Understanding the difference determines whether a missing file crashes your app or degrades gracefully.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

include vs require vs Autoloading

include vs require vs Autoloading
<?php
declare(strict_types=1);

// Project structure without Composer:
// /
// ├── config/
// │   └── database.php
// ├── includes/
// │   ├── header.php
// │   └── footer.php
// ├── src/
// │   └── helpers.php
// └── index.php

// ── include - warning if file missing, execution continues ───────
include "includes/optional-widget.php";    // ⚠ Warning but page loads

// ── require - fatal error if file missing, execution STOPS ───────
require "config/database.php";             // Fatal if missing - correct for critical files

// ── _once variants prevent redeclaration errors ──────────────────
require_once "src/helpers.php";   // safe to call multiple times - only executes once
include_once "includes/header.php";

// ── Using __DIR__ for reliable paths ─────────────────────────────
// Relative paths are relative to the CURRENT WORKING DIRECTORY (where PHP runs)
// not to the file - this causes bugs when included from different folders
// ALWAYS use __DIR__ for robust includes:

require_once __DIR__ . "/../config/database.php";
require_once __DIR__ . "/../src/helpers.php";

// ── Simple file organisation pattern (pre-Composer) ──────────────

// config/database.php
// <?php
// return [
//     "host"   => "localhost",
//     "dbname" => "blog",
//     "user"   => "root",
//     "pass"   => "",
// ];

// In main file:
$dbConfig = require __DIR__ . "/config/database.php";
echo $dbConfig["host"];  // "localhost" - require returns the last expression

// ── Template includes ─────────────────────────────────────────────
// includes/header.php has access to variables in scope
$pageTitle = "Blog - PHP Course";
include __DIR__ . "/includes/header.php";  // can use $pageTitle

Quick Quiz

Tip

Tip

Practice include require Code Organisation in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for include, require & Code Organisation

Practice Task

Note

Practice Task - (1) Write a working example of include require Code Organisation from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with include require Code Organisation is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/9 done