DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Understanding the Differences Between include, require, include_once, and require_once in PHP

When working with PHP, one of the common tasks you will encounter is including external files into your scripts. PHP provides several mechanisms for this task, namely include, require, include_once, and require_once. These statements are essential in modularizing code and enabling file reuse across various parts of an application. However, understanding the differences between these commands is crucial for writing efficient and maintainable PHP code.

This article will walk you through each of these statements, explain their behavior, highlight their differences, and provide practical use cases.


1. The include Statement

What is include?

The include statement in PHP is used to include and evaluate the specified file during the execution of the script. If the file is found, it is included once and executed at that point in the script.

Behavior of include:

  • If the specified file is not found, PHP issues a warning (E_WARNING) but continues script execution.
  • The warning message will include the path of the file that could not be found.
  • It does not stop the execution of the script, so if the included file is not critical, the script can continue running without interruption.

Use Case of include:

You might use include when a file is not critical to the program’s flow and it’s acceptable to continue the script even if the file is missing. This is often used for non-essential files such as optional templates, configuration files, or logging mechanisms.

Example:

// Including a non-critical file
include 'header.php';  // This will continue if header.php is missing
echo "This part of the script will run regardless of the missing header file.";
Enter fullscreen mode Exit fullscreen mode

Why Use include?

  • Useful when including optional files like page templates or non-essential configurations.
  • Allows the script to continue functioning even if the file cannot be included.

2. The require Statement

What is require?

Like include, the require statement is used to include and evaluate a file in PHP. However, the major difference is in how errors are handled.

Behavior of require:

  • If the file is not found or cannot be included, PHP will issue a fatal error (E_COMPILE_ERROR), and the script will stop execution immediately.
  • Unlike include, the missing file will halt the script if it’s critical.

Use Case of require:

You should use require when the included file is essential for the application’s functionality. For instance, a configuration file that sets up constants or includes important functions for your application should be included with require. If the file is missing, continuing the execution could lead to unpredictable behavior or failure.

Example:

// Including a critical file
require 'config.php';  // This will stop the script if config.php is missing
echo "This will not run if config.php is not found.";
Enter fullscreen mode Exit fullscreen mode

Why Use require?

  • When the included file is essential for the script's functioning, like configuration files or database connection scripts.
  • You want to ensure that the script stops executing if the file is missing, to avoid unexpected errors or crashes later in the script.

3. The include_once Statement

What is include_once?

The include_once statement is similar to the include statement, with one key difference: it ensures that the file is included only once during the script’s execution, no matter how many times the include_once statement is called in the code.

Behavior of include_once:

  • It will attempt to include the file just like include.
  • If the file has already been included before in the current script, it will not include it again.
  • If the file cannot be found, it will issue a warning, just like include, but the script continues running.

Use Case of include_once:

You would typically use include_once when including files that may contain functions or class definitions that should only be included once, regardless of how many times you call the inclusion. For instance, you wouldn’t want to include a file that defines a class multiple times, as this could lead to redefinition errors.

Example:

// Ensuring a file is included only once
include_once 'functions.php';  // The file will be included only once
include_once 'functions.php';  // This will be ignored, file not included again
echo "The functions file is included once.";
Enter fullscreen mode Exit fullscreen mode

Why Use include_once?

  • Prevents the inclusion of a file multiple times.
  • Useful when defining functions, classes, or constants in a file that should only be included once, like utility files or configuration files.

4. The require_once Statement

What is require_once?

The require_once statement works similarly to require, but with the additional behavior of ensuring the file is included only once during the script’s execution.

Behavior of require_once:

  • It will attempt to include the file just like require.
  • If the file has already been included, it will not include it again, preventing redefinition errors for classes, functions, or constants.
  • If the file is missing, it will cause a fatal error, just like require, halting the execution of the script.

Use Case of require_once:

You should use require_once when including essential files that must be included only once, such as database connection files, configuration files, or class definitions. It is the most robust and secure way to ensure that critical files are included only once without the risk of redefinition.

Example:

// Ensuring a critical file is included only once
require_once 'database.php';  // Will include only once, even if called multiple times
require_once 'database.php';  // This will be ignored
echo "The database file is included once.";
Enter fullscreen mode Exit fullscreen mode

Why Use require_once?

  • When you need to include files that are critical for the script and ensure they are included only once, such as configuration files or class definitions.
  • Prevents redefinition errors by ensuring that the file is not included multiple times.

Comparison of include, require, include_once, and require_once

Statement Behavior if File is Missing Includes Only Once Error Type
include Warning, continues script No Warning (E_WARNING)
require Fatal error, halts script No Fatal error (E_COMPILE_ERROR)
include_once Warning, continues script Yes Warning (E_WARNING)
require_once Fatal error, halts script Yes Fatal error (E_COMPILE_ERROR)

Key Takeaways:

  • include: Use when the file is optional, and missing files should not halt the script.
  • require: Use when the file is critical, and the script should stop if the file is missing.
  • include_once: Use when the file is optional but should only be included once to avoid duplication.
  • require_once: Use when the file is critical and must be included only once.

Conclusion

Choosing the right inclusion statement depends on the nature of the file you're including and the behavior you want to enforce. require and require_once are typically used for essential files, while include and include_once are more suitable for non-critical files. Using once versions of these statements helps prevent issues like redefinition errors in case of multiple inclusions.

By understanding these differences, you can write more reliable, modular, and error-free PHP code, ensuring that your application functions correctly even when dealing with missing or duplicated files.

Top comments (0)