PHP include Statement
last modified April 16, 2025
The PHP include
statement allows inserting the content of one PHP
file into another before execution. This enables code reuse and modular
programming. Included files share the same variable scope as the including file.
Basic Definitions
The include
statement includes and evaluates the specified file.
If the file is not found, PHP emits a warning but continues execution.
Related statements are require
, include_once
, and
require_once
. Require stops execution if the file is missing.
The *_once variants prevent multiple inclusions of the same file. This avoids function redefinitions and variable reassignments from duplicate includes.
Basic File Inclusion
This example demonstrates including a simple header file in a main page.
<?php declare(strict_types=1); include 'header.php'; echo "<main>Welcome to our website!</main>"; include 'footer.php';
<!DOCTYPE html> <html> <head> <title>My Website</title> </head> <body> <header> <h1>Website Header</h1> </header>
The main file includes both header and footer files. The included files become part of the final output. This allows consistent headers/footers across pages. Path can be relative or absolute.
Including Configuration Files
This example shows including a configuration file with settings and constants.
<?php declare(strict_types=1); const DB_HOST = 'localhost'; const DB_NAME = 'mydb'; const DB_USER = 'admin'; const DB_PASS = 'secret';
<?php declare(strict_types=1); include 'config.php'; $connection = new PDO( "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS ); echo "Connected to database: " . DB_NAME;
The config file defines database constants. The main app file includes it to access these settings. This centralizes configuration management. Changes only need to be made in one file.
Including Function Libraries
This example demonstrates including a file containing reusable functions.
<?php declare(strict_types=1); function formatDate(string $date): string { return date('F j, Y', strtotime($date)); } function sanitizeInput(string $input): string { return htmlspecialchars(trim($input)); }
<?php declare(strict_types=1); include 'functions.php'; $userInput = " <script>alert('xss')</script> "; $cleanInput = sanitizeInput($userInput); $joinDate = '2023-04-15'; echo "Member since: " . formatDate($joinDate);
The functions file contains utility functions. The profile page includes it to use these functions. This promotes code reuse across multiple pages. Functions become available to the including file.
Conditional Includes
This example shows how to include files based on certain conditions.
<?php declare(strict_types=1); $userRole = 'admin'; if ($userRole === 'admin') { include 'admin-menu.php'; } else { include 'user-menu.php'; } echo "<div class='content'>Main page content</div>";
<nav> <a href='dashboard.php'>Dashboard</a> <a href='users.php'>Manage Users</a> <a href='settings.php'>Settings</a> </nav>
The page includes different menu files based on user role. This allows dynamic content inclusion. The condition determines which file gets included. All variables are available to included files.
Include Path Configuration
This example demonstrates setting include paths and using absolute paths.
<?php declare(strict_types=1); // Add directory to include path set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/includes'); // Now can include files from that directory include 'utilities.php'; // Or use absolute path include __DIR__ . '/templates/header.php'; echo "Application initialized";
The script configures additional include paths. Files can then be included from
these paths without full paths. __DIR__
provides the current
directory. This makes includes more portable across environments.
Include vs Require
This example compares include and require statements and their behavior.
<?php declare(strict_types=1); // Will emit warning but continue execution include 'nonexistent.php'; echo "This still executes after include failure"; // Will halt execution with fatal error require 'nonexistent.php'; echo "This line won't be reached";
Include emits warnings for missing files but continues. Require stops execution for missing files. Choose based on file importance. Critical files should use require. Optional files can use include.
Include_once Example
This example demonstrates using include_once to prevent multiple inclusions.
<?php declare(strict_types=1); function initializeApp() { echo "Application initialized\n"; } initializeApp();
<?php declare(strict_types=1); include_once 'init.php'; include_once 'init.php'; // Won't be included again echo "Main application code";
The init.php file contains initialization code. Main.php includes it twice but with include_once. The second inclusion is skipped. This prevents function redefinition errors. Useful for files that should only load once.
Best Practices
- Security: Validate file paths to prevent directory traversal.
- Organization: Keep included files in a dedicated directory.
- Performance: Use opcache for frequently included files.
- Error Handling: Check if critical files exist before including.
- Naming: Use clear naming conventions for included files.
Source
This tutorial covered PHP file inclusion with practical examples showing include usage in various scenarios and best practices.
Author
List all PHP basics tutorials.