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.
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.
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.
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.
Let us start with a tiny file called header.php. It holds a simple heading and a menu.
<?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
// 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.
require is written the same way. It also loads another file and runs it.
<?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.
Let us try loading a file that does not exist, once with each statement.
<?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.
| Statement | File missing? | Script continues? | Good for |
|---|---|---|---|
| include | Warning | Yes | Optional parts, like a sidebar or banner |
| require | Fatal error | No | Essential parts, like settings or database code |
| include_once | Warning | Yes | Optional files that must load only one time |
| require_once | Fatal error | No | Essential files that must load only one time |
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
// 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
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.
A simple rule works well for beginners:
Here is how a simple page can use all of this together.
<?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 haveThe 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.
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.
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.
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.
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.
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 BeginnersNeed a website or web app built with clean, well-organised PHP? Fepiq can help you plan and build it.
Talk to FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.