KOSUNI KOREA
Korean News in English
Technology

Building a PHP Project Structure with include and require

Published October 18, 2026 · 1 views
Building a PHP Project Structure with include and require

[PHP 강의] PHP 8.4 웹 프로그래밍 강의를 시작하며 – 초보도 끝까지 완주할 수 있는 실전 강의


What Problems Arise as Your Project Grows?

At first, a single index.php file may suffice.

However, as features are added one by one, the situation changes.

For example:

  • Header

  • Menu

  • Footer

  • Database connection

  • Login verification

  • Common functions

These elements would repeat across all pages.

If the same code were written in 100 files, you'd need to make 100 edits when updating it.

To solve this problem, we use include and require.


What is include?

include allows you to load another PHP file into the current one.

In simple terms, it lets you treat multiple files as if they were a single file.

Let's look at a basic example.


Creating Common Files

header.php

<h1>PHP Web Programming</h1>
<hr>

index.php

<?php

include "header.php";

echo "Main page.";

Result:

PHP Web Programming

---------------------

Main page.

The content of header.php is inserted directly.


Separating the Footer

footer.php

<hr>

Copyright 2026 BlogScore

index.php

<?php

include "header.php";

echo "Content.";

include "footer.php";

This way, the same footer can be used across all pages.


What is require?

The usage is nearly identical to include.

<?php

require "header.php";

However, there's an important difference.


include vs. require

If the file doesn't exist:

include

include "menu.php";

An error occurs but the program continues running.

Whereas:

require "menu.php";

Causes a fatal error and stops the program immediately.


When to Use include

Use include for files that can be missing without breaking the page:

  • Advertisement areas

  • Announcements

  • Sidebar content

  • Recent posts list

These are files that aren't essential for the page to function.


When to Use require

Use require for files that must exist for proper operation:

  • Database connection

  • Configuration files

  • Common functions

  • Login verification

These are critical files without which the program cannot run properly.


include_once

Prevents a file from being included multiple times.

<?php

include_once "header.php";

If the file is already included, it won't be loaded again.


require_once

This is the most commonly used approach in practice.

<?php

require_once "config.php";

In real-world development, require_once is almost always used because it prevents duplicate includes and ensures essential files are loaded.


Building a Project Structure

In practice, projects are often organized into folders like this:

project/

├── config/

├── inc/

├── uploads/

├── css/

├── js/

├── images/

├── index.php

├── login.php

└── board.php

Separating by functionality makes management much easier.


config Folder

Stores configuration files.

For example:

config/

    db.php

    config.php

This manages database connection details and site settings.


inc Folder

Stores common files.

inc/

    header.php

    footer.php

    menu.php

These are files used across all pages.


Separating Database Connection

db.php

<?php

$pdo = new PDO(

    $dsn,

    $user,

    $password

);

When using it:

<?php

require_once "config/db.php";

This single line loads the database connection.


Creating Common Functions

function.php

<?php

function h($text)
{

    return htmlspecialchars(

        $text,

        ENT_QUOTES,

        "UTF-8"

    );

}

Usage:

<?php

require_once "function.php";

echo h("<script>");

This will be used frequently in future board projects.


Automatically Connecting Header and Footer

<?php

require_once "inc/header.php";

echo "Content";

require_once "inc/footer.php";

All pages can now use the same design.


Managing Menus in One Place

menu.php

<ul>

<li>Home</li>

<li>Board</li>

<li>Sign Up</li>

</ul>

On all pages, simply include it:

include "inc/menu.php";

Editing the menu only requires changing one file.


Creating a Login Verification File

auth.php

<?php

session_start();

if (!isset($_SESSION["userid"])) {

    exit("Login required.");

}

For member-only pages:

require_once "auth.php";

This single line adds login verification.


Writing Paths

If in the same folder:

include "header.php";

If in another folder:

include "inc/header.php";

You can also use absolute paths.

require_once __DIR__ . "/inc/header.php";

In practice, __DIR__ is widely used because it's safe even if folder locations change.


Using __DIR__

<?php

require_once __DIR__ . "/config/db.php";

This is the recommended approach for PHP projects.


Real-World Example

Let's build a board index page.

<?php

require_once __DIR__ . "/config/db.php";

require_once __DIR__ . "/inc/header.php";

echo "<h2>Board</h2>";

require_once __DIR__ . "/inc/footer.php";

This structure closely matches real board projects.


Common Mistakes for Beginners

New users often make these mistakes with include:

  • Miswriting file paths

  • Mixing up include and require

  • Including the same file multiple times

  • Errors due to relative paths

  • Not using __DIR__

In practice, the combination of require_once + __DIR__ is most commonly used.


What's Next for Your Board Project?

The structure we've built will be used throughout future projects.

For example:

  • Database connection

  • Login verification

  • Common functions

  • Header

  • Footer

  • Admin menu

All will be separated into individual files.

This makes the code cleaner and much easier to maintain.


In Conclusion

This lesson covered building a project structure using include and require.

The ability to properly separate files becomes increasingly important as your project grows.

Especially, getting comfortable with the require_once and __DIR__ approach is crucial for real-world PHP development.

In the next lesson, we'll learn how to connect to MySQL and store data in PHP, building a full database-driven web application.

FAQ

What is the difference between include and require?

include continues execution if a file is missing, while require causes a fatal error and stops the program.

When should I use require_once instead of require?

Use require_once to prevent duplicate includes of the same file. It's the standard practice in professional PHP development.

Korean Source

This article is an English localization of a Korean-language report from BlogScore. Original: [PHP 강좌] 8강 include, require를 이용한 프로젝트 구조 만들기