PHP offers several ways to include external files into your scripts. The most common are include
, include_once
, require
, and require_once
. These functions all take a filename as an argument and insert the contents of that file into the current script at the point where the function is called.
The primary difference lies in how they handle errors and whether they allow multiple inclusions:
include
: This inserts the specified file. If the file is not found, it generates a warning, but the script continues execution. This is useful when the included file is optional.include_once
: This is similar to include
, but it only includes the specified file once. If the file has already been included, it's skipped, preventing duplicate code execution. This is useful for preventing conflicts if a file contains functions or classes that might be redefined.require
: This is also used to include a file, but it generates a fatal error if the file is not found. This means the script will halt execution. Use require
when the included file is essential for the script's functionality.require_once
: Similar to require
, but it ensures the file is included only once. If the file has already been included, it's skipped, preventing duplicate code. This is often the preferred method for including critical files to avoid errors and maintain code integrity.Here's a simple example demonstrating include
:
<?php include 'my_file.php'; // my_file.php contains some code echo "This code executes after including my_file.php"; ?>
And an example using require
:
<?php require 'essential_file.php'; // essential_file.php contains critical code echo "This code only executes if essential_file.php is found"; ?>
Remember to replace 'my_file.php'
and 'essential_file.php'
with the actual paths to your files.
include
, include_once
, require
, and require_once
in PHP 7?The key differences boil down to error handling and multiple inclusion prevention:
Function | Error Handling | Multiple Inclusion | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Warning | Allowed | |||||||||||||||
<🎜> | Warning | Prevented | |||||||||||||||
<🎜> | Fatal Error | Allowed | |||||||||||||||
<🎜> | Fatal Error | Prevented |
In essence:
include
and include_once
are for optional files; the script continues even if the file is missing.require
and require_once
are for essential files; the script stops if the file is missing._once
variants prevent duplicate inclusions, which is crucial for avoiding conflicts with function or class definitions. This is generally the preferred practice for robust code.Effective error handling when including files is crucial for creating robust applications. While require
and require_once
inherently halt execution on failure, you can enhance error handling using set_error_handler
to customize how errors are reported. For include
and include_once
, you can check the return value, which will be FALSE
if the file was not included successfully.
Here's an example using set_error_handler
:
<?php include 'my_file.php'; // my_file.php contains some code echo "This code executes after including my_file.php"; ?>
This example defines a custom error handler that logs inclusion errors. It also demonstrates checking the return value of include_once
. Remember to restore_error_handler()
to avoid interfering with other parts of your application. For production environments, consider logging errors to a file instead of displaying them directly to the user.
Organizing files effectively is vital for maintainability in large projects. Consider these best practices:
By implementing these practices, you can effectively manage and organize included and required files, leading to a more maintainable, scalable, and robust PHP application.
The above is the detailed content of How to Include and Require Files in PHP 7?. For more information, please follow other related articles on the PHP Chinese website!