The include and require statements in PHP are used to include and evaluate files within a script. These statements allow better code reusability by enabling a single PHP file to be reused across multiple scripts.
include – Generates a warning if the file is missing and continues execution.require – Generates a fatal error if the file is missing and stops execution.include_once – Includes a file only once.require_once – Requires a file only once and stops execution if missing.These statements are commonly used to load headers, footers, configuration files, database connections, and reusable logic.
// Includes header.php and continues even if file is missing
<?php
include 'header.php';
echo "This is the body of the page.";
?>
The file header.php is inserted at runtime. If it does not exist, PHP shows a warning but executes the remaining script.
// Requires config.php and stops execution if file is missing
<?php
require 'config.php';
echo "This is the body of the page.";
?>
If config.php is missing, PHP throws a fatal error and the script stops immediately.
// Ensures header.php is included only once
<?php
include_once 'header.php';
include_once 'header.php';
echo "This is the body of the page.";
?>
Even though the statement appears twice, the file is included only once, preventing duplicate content.
// Loads critical configuration file only once
<?php
require_once 'config.php';
require_once 'config.php';
echo "Configuration loaded successfully.";
?>
Request → PHP Script → include/require → External File → Output
This flow shows how PHP dynamically inserts external files before producing the final output.
require for critical files like database configurations.include for optional components.require_once for files that must load only once.include.require with a missing file.include_once to avoid duplicate content.require_once for a database connection file.