All posts
PHPOctober 31, 20267 min read

PHP include vs require Explained for Beginners

PHP include vs require explained in plain English. Learn the one key difference, include_once and require_once, and when to use each, with simple examples.

F
Fepiq Team
Fepiq

What is the difference between PHP include and require? The short answer: both load another PHP file into your current file. If the file is missing, include shows a warning and your page keeps running. require stops the page completely with a fatal error.

That one difference decides which one you should pick. In this beginner guide you will learn what both do, see small examples you can copy, and learn the _once versions, include_once and require_once. No programming experience is needed.

Why do we need include and require?

Imagine a website with ten pages. Every page has the same menu at the top and the same footer at the bottom. You could copy the menu into all ten files. But when you want to change one link, you must edit ten files. That is slow, and it is easy to make mistakes.

A better way is to write the menu once in its own file. Then each page says, "load that file here". PHP gives you four statements for this: include, require, include_once and require_once. A statement is just one instruction for PHP.

How PHP include works

Let us start with a tiny file called header.php. It holds a simple heading and a menu.

php
<?php
// header.php
echo '<h1>My Website</h1>';
echo '<a href="/">Home</a> | <a href="/about.php">About</a>';

Line 1 opens PHP. Line 2 is a comment, which PHP ignores. The two echo lines print HTML to the page. Now we load this file from index.php.

php
<?php
// index.php
include 'header.php';

echo '<p>Welcome to the home page!</p>';

The line include 'header.php'; tells PHP to open header.php and run everything inside it right at that spot. The page then shows the heading, the menu, and finally the welcome text. Both files must be in the same folder for this short path to work.

How PHP require works

require is written the same way. It also loads another file and runs it.

php
<?php
// index.php
require 'config.php';

echo 'Site name: ' . $siteName;

Here config.php might hold settings such as $siteName = 'My Website';. The dot (.) joins two pieces of text together. Our page needs $siteName to work. If config.php is missing, there is no point in going on, and that is exactly when require is the right choice.

The key difference: what happens when the file is missing

Let us try loading a file that does not exist, once with each statement.

php
<?php
include 'missing.php';
echo 'This line still runs.';

require 'missing.php';
echo 'This line will never run.';

The first include cannot find missing.php, so PHP shows a warning. A warning is a message that says something went wrong, but the script continues. So 'This line still runs.' is printed. The require then fails with a fatal error. A fatal error stops the script at once, so the last echo never happens.

StatementFile missing?Script continues?Good for
includeWarningYesOptional parts, like a sidebar or banner
requireFatal errorNoEssential parts, like settings or database code
include_onceWarningYesOptional files that must load only one time
require_onceFatal errorNoEssential files that must load only one time

What do include_once and require_once do?

Sometimes the same file gets loaded twice by accident. Say index.php loads functions.php, and header.php also loads functions.php. If that file defines a function, PHP will complain because you cannot define the same function two times. Let us see it.

php
<?php
// functions.php
function sayHello() {
    echo 'Hello!';
}

This file defines one function named sayHello. A function is a named block of code you can run whenever you want. Now we load it two times.

php
<?php
require_once 'functions.php';
require_once 'functions.php'; // skipped, already loaded

sayHello();

The first require_once loads the file. The second one sees the file is already loaded and quietly skips it. So there is no error, and sayHello() prints Hello!. If you had used plain require twice, PHP would stop with an error about redeclaring the function.

Which one should you use?

A simple rule works well for beginners:

  • Use require_once for files your page cannot work without, such as settings, database connection and function files.
  • Use include for parts that are nice to have, such as a banner, an ad box or a sidebar.
  • Use the plain require or include when you want the file to run every time, for example a small template piece that you repeat.
  • When in doubt, choose require_once. It fails loudly, which helps you spot mistakes early.

A real example: a small page with shared parts

Here is how a simple page can use all of this together.

php
<?php
require_once 'config.php';      // must exist
require_once 'functions.php';   // must exist
include 'header.php';           // nice to have

echo '<p>' . greet($siteName) . '</p>';

include 'footer.php';           // nice to have

The first two lines load files the page truly needs, so if either is missing, PHP stops. The header and footer are included, so if one is missing you get a warning but the main content still shows. The greet() function is assumed to be defined in functions.php. Everything after the double slash // is a comment for humans.

Common mistakes and how to fix them

  • Wrong path: if the file is in another folder, write the folder too, like 'includes/header.php'. A path is the address of a file.
  • Typos in the file name: header.php and Header.php are different on many servers. Match the name exactly.
  • Using parentheses: include('file.php') works, but they are not needed. Most people write include 'file.php';.
  • Forgetting the semicolon at the end of the line. Every PHP statement ends with ;.
  • Putting secret passwords in a file that visitors can open directly. Keep settings files outside the public folder when you can.

Practice ideas

  1. Create header.php and footer.php, then include them in two different pages.
  2. Create config.php with a variable and use require_once to read it.
  3. Rename a file on purpose and watch how include and require react differently.
  4. Load the same file twice with include_once and see that nothing breaks.

Frequently asked questions

What is the difference between include and require in PHP?+

Both load and run another PHP file. If the file cannot be found, include gives a warning and the script keeps going, while require gives a fatal error and stops the script. Use require for files your page cannot work without.

Is require faster than include?+

No, the speed difference is so small that you will never notice it. Choose between them based on what should happen when the file is missing, not on speed.

When should I use require_once instead of require?+

Use require_once when a file might be loaded more than once, such as a file with functions or a database connection. PHP will load it only the first time and skip it after that, which avoids errors like redeclared functions.

Can I include a file from another folder in PHP?+

Yes. Write the path to the file, for example include 'includes/menu.php';. You can also use __DIR__ . '/includes/menu.php' to build the path from the folder of the current file, which is more reliable.

Can I use include and require for HTML files?+

Yes. PHP will print any plain HTML in the file as it is, and run any PHP code inside <?php ?> tags. This is a popular way to share a menu or footer across pages.

Want to write reusable code the next step? Learn how to build your own named blocks of code in our PHP functions guide.

Read: PHP Functions for Beginners

Need a website or web app built with clean, well-organised PHP? Fepiq can help you plan and build it.

Talk to Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.