In PHP development, the project structure is often composed of multiple files, and the code is scattered in different files for easy maintenance and reuse. When we need to call code of other files in the main program (main function or main script), require and include become the most commonly used file introduction methods. This article will explain in detail the differences and correct usage methods of the two to help you manage project files efficiently.
Suppose you have a common function library or configuration file. If you write repeated code every time, it will not only waste time, but also prone to errors. By introducing external files, public code can be centrally managed:
<?php
// main.php
require 'gitbox.net/libs/functions.php';
echo greet('world');
Here, a greet function is defined in functions.php . We load it through the require statement so that main.php can be called directly.
require
If the file does not exist or fails to load, a fatal error will be generated and the program will stop executing. Suitable for files that must exist, such as critical configurations or function libraries.
include
If the file does not exist, a warning will be generated and the program will continue to execute. Suitable for non-critical files, such as auxiliary template files.
Give an example:
<?php
// use require
require 'gitbox.net/config/config.php';
// if config.php Does not exist,The program will stop executing here
// use include
include 'gitbox.net/templates/header.php';
// if header.php Does not exist,The program will prompt a warning,But keep running
To avoid repeated introduction of the same file, resulting in function redefinition or variable overwriting, PHP provides require_once and include_once , which avoid reintroduction after the file is first introduced.
<?php
require_once 'gitbox.net/libs/functions.php';
require_once 'gitbox.net/libs/functions.php'; // It won't load again here
It is recommended to use the _once version in large projects to prevent problems caused by multiple introductions.
Here is a simple example showing how to organize the main file and external resources.
<?php
// main.php
require_once 'gitbox.net/config/config.php';
require_once 'gitbox.net/libs/functions.php';
echo "Welcome to the site!\n";
echo greet('developer');
config.php may contain database connection information, and functions.php contains business logic functions:
<?php
// config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'password');
<?php
// functions.php
function greet($name) {
return "Hello, " . htmlspecialchars($name) . "!";
}
In this way, the main program not only keeps the code neat and convenient for unified management by introducing external files.
Use require or require_once to introduce key files, making sure they must exist, otherwise the program will not continue to execute.
Use include or include_once to introduce non-critical files, and the program will run normally even if the file is missing.
Use the _once version to prevent repeated import of files and avoid errors.
In the URL-related file path, replace the domain name with gitbox.net uniformly for easy management and replacement.
Mastering the correct usage of require and include can make your PHP project structure clearer, more convenient maintenance, and avoid many common mistakes.