Introduction
Include and require statements in PHP allow you to reuse PHP code from one file in another file. They are commonly used to manage website headers, footers, navigation menus, database connections, and common functions. In this chapter, you will practise both statements through practical PHP examples and learn how they handle missing files.
Question 1: Include a PHP File
Problem Statement:
Create two PHP files. Store a welcome message in one file and display it by including that file in another PHP file.
PHP Solution:
welcome.php
<?php
echo "Welcome to PHP Programming!";
?>
index.php
<?php
include "welcome.php";
?>
Sample Output:
Welcome to PHP Programming!
Explanation:
welcome.phpcontains the welcome message.index.phpuses theincludestatement to load that file.- The message from
welcome.phpis displayed whenindex.phpruns.
Concepts Covered: Include statement, file inclusion, code reuse.
Question 2: Include a Header File
Problem Statement:
Create a separate header file and include it in a PHP page to display a common website heading.
PHP Solution:
header.php
<?php
echo "<h1>VSIT Coding Institute</h1>";
echo "<p>Learn Programming Skills</p>";
?>
index.php
<?php
include "header.php";
echo "<h2>Home Page</h2>";
?>
Sample Output:
VSIT Coding Institute
Learn Programming Skills
Home Page
Explanation:
header.phpcontains the common website heading.index.phpincludes the header file.- The home page content appears after the included header.
- The same header can be reused on multiple website pages.
Concepts Covered: Header files, include statement, reusable website components.
Question 3: Include a Footer File
Problem Statement:
Create a footer file containing copyright information and include it at the bottom of a PHP page.
PHP Solution:
footer.php
<?php
echo "<footer>";
echo "Copyright 2026 VSIT Coding Institute";
echo "</footer>";
?>
index.php
<?php
echo "<h1>About Us</h1>";
echo "<p>Welcome to our website.</p>";
include "footer.php";
?>
Sample Output:
About Us
Welcome to our website.
Copyright 2026 VSIT Coding Institute
Explanation:
footer.phpstores the common footer content.index.phpdisplays the main page content.- The footer is included after the main content.
- You can reuse the same footer on different pages.
Concepts Covered: Footer files, include statement, reusable website layout.
Question 4: Include a File Containing PHP Variables
Problem Statement:
Create a PHP file containing website details and access those variables from another PHP file using include.
PHP Solution:
config.php
<?php
$websiteName = "VSIT";
$websiteEmail = "info@vsit.in";
?>
index.php
<?php
include "config.php";
echo "Website: " . $websiteName . "\n";
echo "Email: " . $websiteEmail;
?>
Sample Output:
Website: VSIT
Email: info@vsit.in
Explanation:
config.phpcontains two variables.index.phpincludes the configuration file.- The included variables become available in the including file’s scope.
- This approach helps manage common website settings in one place.
Concepts Covered: Configuration files, variable sharing, include statement.
Question 5: Use the Require Statement
Problem Statement:
Create a PHP file containing a message and load it into another file using the require statement.
PHP Solution:
message.php
<?php
echo "PHP makes website development easier.";
?>
index.php
<?php
require "message.php";
echo "\nWelcome to PHP.";
?>
Sample Output:
PHP makes website development easier.
Welcome to PHP.
Explanation:
message.phpcontains a message.requireloads the file intoindex.php.- The included message is displayed before the next statement.
requireis useful when a file is essential for the application to work.
Concepts Covered: Require statement, file inclusion, code reuse.
Question 6: Understand the Difference Between Include and Require
Problem Statement:
Write a PHP program to understand how include and require behave when a file is missing.
PHP Solution:
Using include:
<?php
include "missing.php";
echo "Program continues.";
?>
Sample Output:
Warning: include(missing.php): Failed to open stream...
Warning: include(): Failed opening 'missing.php'...
Program continues.
Exact warning text may vary depending on the PHP version and server configuration.
Using require:
<?php
require "missing.php";
echo "Program continues.";
?>
Sample Output:
Fatal error: Uncaught Error: Failed opening required 'missing.php'...
The exact error message may vary. The final echo statement does not execute.
Explanation:
includeproduces a warning when the file is missing, but PHP normally continues executing the remaining code.requireproduces an error that stops normal execution when the file cannot be loaded.- Use
requirefor essential files andincludewhen the file is not critical.
Concepts Covered: Include vs require, missing files, PHP error handling.
Question 7: Include a File Using a Relative Path
Problem Statement:
Create a PHP project with a separate folder containing a message file. Include that file using a relative path.
Folder Structure:
project/
│
├── index.php
│
└── includes/
└── message.php
PHP Solution:
includes/message.php
<?php
echo "This message comes from the includes folder.";
?>
index.php
<?php
include "includes/message.php";
?>
Sample Output:
This message comes from the includes folder.
Explanation:
- The
includesfolder stores reusable PHP files. - The relative path
includes/message.phppoints to the required file. - The
includestatement loads and executes that file. - Organizing reusable files into folders makes projects easier to manage.
Concepts Covered: Relative paths, folder structure, file inclusion.
Question 8: Use Require Once to Prevent Duplicate Inclusion
Problem Statement:
Create a PHP file containing a function and load it using require_once to avoid including the same file multiple times.
PHP Solution:
functions.php
<?php
function welcomeUser() {
echo "Welcome to PHP!";
}
?>
index.php
<?php
require_once "functions.php";
require_once "functions.php";
welcomeUser();
?>
Sample Output:
Welcome to PHP!
Explanation:
functions.phpcontains a reusable function.require_onceloads the file only once during the script execution.- The second
require_oncedoes not load the same file again. - This helps prevent errors such as function redeclaration.
Concepts Covered: Require once, duplicate inclusion prevention, reusable functions.
Question 9: Use Include Once for a Configuration File
Problem Statement:
Create a configuration file containing website details and load it using include_once.
PHP Solution:
config.php
<?php
$company = "VSIT Coding Institute";
$location = "Dwarka, New Delhi";
?>
index.php
<?php
include_once "config.php";
include_once "config.php";
echo "Company: " . $company . "\n";
echo "Location: " . $location;
?>
Sample Output:
Company: VSIT Coding Institute
Location: Dwarka, New Delhi
Explanation:
config.phpstores common website information.include_onceensures the file is included only once during script execution.- The second inclusion is skipped.
- This is useful for configuration files that may be needed in multiple parts of a project.
Concepts Covered: Include once, configuration files, duplicate inclusion prevention.
Question 10: Build a Reusable Website Layout Using Include and Require
Problem Statement:
Create a simple website layout using separate header, navigation, content, and footer files. Use require and include to combine them into one page.
Folder Structure:
project/
│
├── index.php
│
└── includes/
├── header.php
├── navigation.php
├── content.php
└── footer.php
PHP Solution:
includes/header.php
<?php
echo "<h1>VSIT Coding Institute</h1>";
?>
includes/navigation.php
<?php
echo "<nav>Home | Courses | Contact</nav>";
?>
includes/content.php
<?php
echo "<h2>Welcome to Our Website</h2>";
echo "<p>Learn PHP and other programming skills.</p>";
?>
includes/footer.php
<?php
echo "<footer>Copyright 2026 VSIT</footer>";
?>
index.php
<?php
require "includes/header.php";
include "includes/navigation.php";
include "includes/content.php";
include "includes/footer.php";
?>
Sample Output:
VSIT Coding Institute
Home | Courses | Contact
Welcome to Our Website
Learn PHP and other programming skills.
Copyright 2026 VSIT
Explanation:
- Each file contains a separate part of the website.
requireloads the essential header file.includeloads the navigation, content, and footer files.- The files are combined to create one complete webpage.
- If the required header file is missing, the remaining page code will not execute normally.
Concepts Covered: Website layout, reusable components, include, require, project organization.
Key Takeaways
includeloads and executes another PHP file.requireloads an essential PHP file.- A missing file with
includenormally generates a warning, while a missing file withrequirestops normal execution. include_onceprevents the same file from being included more than once.require_onceprevents the same required file from being loaded more than once.- Reusable files help organize headers, footers, navigation menus, and configuration settings.
- Use relative paths or appropriate absolute paths to locate PHP files.
FAQs
1. What is the include statement in PHP?
The include statement loads and executes the code from another PHP file in the current file.
2. What is the require statement in PHP?
The require statement loads another PHP file. It is generally used when the file is essential for the application to work.
3. What is the difference between include and require in PHP?
If a file is missing, include normally produces a warning and allows execution to continue. require produces an error that stops normal execution.
4. What is include_once in PHP?
include_once includes a file only once during a script execution, even if the same statement is encountered again.
5. What is require_once in PHP?
require_once loads an essential file only once and prevents duplicate inclusion.
6. Can we include HTML files in PHP?
Yes. PHP can include HTML files, and their content becomes part of the generated webpage.
7. Where are include and require statements used in real projects?
They are commonly used for website headers, footers, navigation menus, database connections, configuration files, and reusable functions.
8. Can we include a PHP file from another folder?
Yes. You can use a relative or absolute file path to include a PHP file from another folder.
9. What happens if we include the same file multiple times?
Using include multiple times can execute the file repeatedly. If it declares the same function or class again, it may cause a redeclaration error. Use include_once when duplicate inclusion should be prevented.
10. Should we use include or require for database connection files?
Use require or require_once for essential database connection files because the application generally cannot perform database operations without them.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
