PHP File Inclusion: require, include, require_once, include_once

php file inclusion

PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose but behaves differently when it handles the missing files or repeated inclusions.

What Is File Inclusion in PHP?

File inclusion in PHP means you insert the content of one PHP file into another. It lets you reuse code like:

  • Headers
  • Footers
  • Functions
  • Configuration settings

All of them are across multiple pages.

You don’t need to copy the same code into every file you can write it once and include it wherever needed.

PHP gives you four types of file inclusion which are:

  • require
  • include
  • require_once
  • include_once

Here’s how each one works in detail:

PHP require Statement

The require statement includes and runs the content of another PHP file. If the file is missing or has an error, PHP stops the script and shows a fatal error.

Use require when the file is essential for your code to run. For example, if you need a config file or a file that defines key functions, use require to make sure the script doesn’t continue without it.

For example:

require 'config.php';

echo "This will run only if config.php is found.";

If config.php doesn’t exist, PHP throws a fatal error and stops the script immediately.

PHP include Statement

The include statement adds and runs the content of another PHP file, just like require. PHP shows a warning but still runs the script if the file doesn’t exist.

Use include when the file is helpful but not critical. This works well for optional content like ads or widgets.

For example:

include 'sidebar.php';

echo "This will run even if sidebar.php is missing.";

PHP shows a warning but still runs the rest of the script if sidebar.php doesn’t exist.

PHP require_once Statement

The require_once statement includes a file only once, even if it’s called multiple times in the code. PHP skips it if the file has already been included.

Like require, it causes a fatal error if the file is missing. Use require_once when the file is essential and must not be included more than once, such as class definitions or config files.

Example:

require_once 'config.php';
require_once 'config.php'; // This will be ignored

echo "config.php loaded only once.";

This prevents redeclaration errors and keeps your code safe from loading the same file twice.

PHP include_once Statement

The include_once statement works like include, but it ensures the file is included only once. If the file has already been included, PHP ignores subsequent include_once calls.

Use include_once when the file is not critical but should only be included once, such as when it includes a library or a configuration file.

Example:

include_once 'functions.php';
include_once 'functions.php'; // This will be ignored

echo "functions.php loaded only once.";

Like require_once, include_once helps avoid errors like redeclaring functions or classes.

The Differences Between require and include

While both require and include serve the same basic purpose—loading external files—they handle errors differently.

  • Error Handling:
    • require causes a fatal error and stops the script if the file is missing or cannot be loaded.
    • include only triggers a warning but allows the script to continue running.
  • Use Case:
    • Use require for files that are essential to the execution of the script, like configuration files or core functionality.
    • Use include for optional files, where missing content won’t break the script, such as adding a footer or sidebar.

Example:

// The require (fatal error if file is missing)
require 'essential.php'; // If missing, script stops

// The include (warning, script continues)
include 'optional.php';  // If missing, script continues

Wrapping Up

In this guide, you learned how PHP handles file inclusion within:

  • require
  • require_once
  • include
  • include_once

You also explored their differences in behavior and error handling. Here’s a quick recap:

  • require: Includes a file and stops the script if the file is missing or contains an error. Use for essential files.
  • include: Includes a file but allows the script to continue if the file is missing, It shows a warning instead of a fatal error. Use for non-critical files.
  • require_once: Ensures a file is included only once, even if called multiple times. It’s useful for files like class definitions or configuration files.
  • include_once: Similar to include, but ensures a file is included only once to avoid errors like redeclaring functions or classes.

FAQ’s

What is the difference between require and include in PHP?

  • require: Includes a file and causes a fatal error if the file is missing, halting script execution.
  • include: Includes a file but only generates a warning if the file is missing, allowing the script to continue.

What does require_once do in PHP?

require_once includes a file only once during script execution. If the file has already been included, subsequent calls are ignored. This is useful for including files that define functions or classes to prevent redeclaration errors.

When should I use include_once?

Use include_once when you want to include a file only once, even if the inclusion statement appears multiple times in the script. This prevents issues like redeclaring functions or classes.

Can I use require and include multiple times for the same file?

Yes, but it's not recommended. Using require or include multiple times for the same file can lead to redeclaration errors. To prevent this, use require_once or include_once, which ensure the file is included only once.

What happens if a file is missing when using include?

If the file is missing, PHP generates a warning (E_WARNING) but the script continues to execute. This is useful for optional files where the absence doesn't affect the overall functionality.

How does require_once differ from require?

Both require and require_once include a file, but require_once checks if the file has already been included. If it has, PHP skips the inclusion, preventing redeclaration errors.

Similar Reads

Class and Object in PHP: How They Work with a Real Example

Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update…

Polymorphism in PHP: How It Works with Examples

PHP 5 added support for classes and interfaces. This made object-oriented code possible with one feature being polymorphism. That means…

PHP implode Function: Join Array Values into a String

The implode function in PHP appeared to solve a common need—joining array elements into a single string. It helps format…

PHP $_REQUEST: Learn How to Capture User Inputs

Today, we will discuss the most prominent and useful superglobal in PHP, $_REQUEST, to get user input. The $_REQUEST array lets you retrieve…

PHP array_keys: How to Extract Keys in Arrays with Examples

This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a…

PHP str_replace Function: How it Works with Examples

If you find a word that does not fit and want to fix it. You can use the PHP str_replace…

PHP Magic Constants: Simplify Your Code

There are some tools known as "PHP magic constants" that will make your code a little more clever and versatile.…

PHP array_change_key_case: Change Array Key Cases

The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use. Understand the array_change_key_case…

Understanding the PHP Exclusive OR (XOR) Operator

In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…

filter_has_var Function: Check if Input Exists in PHP

User input does not always come through as expected. Sometimes values are missing or not set at all. This can…

Previous Article

Understanding JavaScript Arithmetic Operators

Next Article

forEach Loop in JavaScript: A Complete Guide with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.