include() vs require() in PHP: Difference & Examples

Introduction

In PHP, developers often split their applications into multiple files to make the code more organized, reusable, and easier to maintain. Instead of writing all the code in one file, PHP allows us to include code from other files using include() and require() statements.
These statements help reuse common files such as:

  • Header files
  • Footer files
  • Configuration files
  • Database connection files
  • Navigation menus

In simple terms:

include() and require() are used to insert the content of one PHP file into another PHP file.

What is include() in PHP?

The include() statement is used to include the content of another PHP file into the current file.

Note: If the file cannot be found, include() generates a warning but the script continues to execute.

Syntax of include()


include("filename.php");

or


include "filename.php";

Example of include()

Suppose we have a file named header.php.

header.php


echo "

Welcome to My Website

";

Now we include it in another file.

index.php


include("header.php");
echo "This is the homepage.";

Output

Welcome to My Website This is the homepage.

Explanation

Here:

  • The header.php file is included in index.php.
  • PHP loads the header content first.
  • Then the rest of the script executes normally.

What Happens if the File is Missing and using include()?

If the file does not exist, include() will generate a warning but the script will continue running.

Example


include("menu.php");
echo "Page Loaded Successfully";

Output

Warning: include(menu.php): failed to open stream Page Loaded Successfully

What is require() in PHP?

The require() statement also includes another file into the current script, just like include().

Note: If the specified file is not found, require() generates a fatal error and stops the script execution immediately.

Syntax of require()


require("filename.php");

or


require"filename.php";

Example of require()

header.php


echo "

Welcome to My Website

";

index.php


require("header.php");
echo "This is the homepage.";

Output

Welcome to My Website This is the homepage.

The output is the same as include() when the file exists.

What Happens if the File is Missing and using require()?

If the required file does not exist, PHP will generate a fatal error and stop execution.
Example


require("menu.php");
echo "Page Loaded Successfully";

Output

Fatal error: Failed opening required ‘menu.php’

The message “Page Loaded Successfully” will not be printed because the script stops immediately.

Difference Between include() and require()

Both include() and require() are used to insert files into a PHP script, but their behavior is different when an error occurs.

Feature include() require()
Purpose Includes a file Includes a file
Error Handling Generates a warning Generates a fatal error
Script Execution Continues running Stops execution
Use Case Optional files Mandatory files

When Should You Use include()?

You should use include() when the file is not critical for the application.

For example:

  • Optional content
  • Sidebar
  • Advertisement section
  • Non-essential UI components

Example:


include("sidebar.php");

Even if the sidebar file is missing, the page can still load.

When Should You Use require()?

You should use require() when the file is essential for the program to run.

For example:

  • Database connection file
  • Configuration file
  • Authentication system
  • Core application files

Example:


require("config.php");

If this file is missing, the application cannot function properly.

Real-Life Example

Most PHP websites use separate files for header, footer, and navigation menus.

header.php


<header>
<h1>My Website</h1>
</header>

footer.php


<footer>
<p>Copyright 2025</p>
</footer>

index.php


include("header.php");
echo "Welcome to our website.";
include("footer.php");

Output

My Website Welcome to our website. Copyright 2025

This approach helps keep the code clean and reusable.

include_once() and require_once()

PHP also provides two additional statements:

  • include_once()
  • include_once()

These ensure that a file is included only once, even if the statement is called multiple times.
Example:


include_once("config.php");

This prevents duplicate file loading, which can cause errors.

Conclusion

The include() and require() statements in PHP are powerful tools that help developers organize code and reuse common files. They make applications more structured and easier to maintain.


Both statements work similarly, but the main difference lies in error handling. The include() statement generates a warning and continues execution, while require() stops the program if the file is missing.

Difference between include(), include_once(), require(), require_once() – Objective Questions (MCQs)

Q1. Which statement produces a fatal error if the file is missing?






Q2. Which allows multiple inclusions of the same file?






Q3. include_once() ensures the file is included ______.






Q4. If a file is missing, include() produces ______.






Q5. Which keyword prevents repeated inclusion of a file?