Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 20

include vs require in PHP

In this lesson: Difference between include, require, include_once and require_once in PHP with error behaviour and examples.

Reusing code across files

Big projects split code into many files — a header, a footer, a config, shared functions. include and require pull one file into another so you write shared code once. They do almost the same thing; the difference is what happens when the file is missing.

💡 One-line rule: include = warning if missing (keeps going); require = fatal error (stops).

include

<?php
  include "header.php";   // pulls in header.php here
  echo "Page content";
?>

If header.php exists, its code runs at that point. If it is missing, PHP shows a warning but continues to "Page content". Good for optional pieces.

require

<?php
  require "config.php";   // essential file
  echo "This only runs if config.php loaded";
?>

If config.php is missing, require throws a fatal error and the script stops immediately — the echo never runs. Use it for files the page truly needs.

The key difference

Pointincluderequire
If file missingWarningFatal error
Script continues?YesNo, it stops
Best forOptional partsEssential files

Choose based on importance: a missing sidebar should not break the page (include), but a missing database config should stop everything (require).

include_once and require_once

These load a file only once, even if the line runs again.

<?php
  require_once "functions.php";
  require_once "functions.php";  // ignored the second time
?>

If a file defines functions or classes, including it twice causes a "cannot redeclare" error. The _once versions prevent this by loading it a single time.

Common mistakes

  • Using include for essential files. A missing config then only warns, and the page breaks later in a confusing way — use require.
  • Double-including function files. Causes "cannot redeclare"; use require_once.
  • Wrong file path. Paths are relative to the current script — use __DIR__ for reliability.
  • Expecting a return value. These load a file; to get a value, have the file return it and capture it.

Summary

  • include and require load one PHP file into another.
  • On a missing file, include warns and continues; require stops with a fatal error.
  • Use require for essential files, include for optional parts.
  • require_once/include_once load a file only once.
  • Use __DIR__ for reliable file paths.

Next, organise larger apps with autoloading, or structure code with the MVC pattern.

Frequently Asked Questions

What is the difference between include and require?
Both load another PHP file into the current one. The difference is on failure: if the file is missing, include only gives a warning and the script keeps running, while require gives a fatal error and stops the script. Use require for essential files.
When should I use require instead of include?
Use require for files the page cannot work without — like a database config or core functions. Use include for optional parts, like a sidebar, where a missing file should not crash the whole page.
What do include_once and require_once do?
They work like include and require but load the file only once, even if called again. This prevents "cannot redeclare" errors when the same file (with functions or classes) might be included more than once.
Why do I get a "cannot redeclare function" error?
It usually means a file with a function or class was included twice. Switch to require_once (or include_once) so PHP loads it only the first time.
Can I include a file inside a function?
Yes. When you include a file inside a function, its variables follow the function's scope. This is one reason to prefer returning values or using clear structure over relying on included variables.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicNamespaces Your progress: 20 / 50 (40%) · Lesson 21 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free