PHP Include Files

This article will examine PHP include and require methods, as well as their syntax, behavior, and explore them through some examples.

Includes and requires, which allow you or any developer to easily reuse code.

In web development, it is common to use these functions to include headers and footers as part of multiple pages, as well as to reuse code across multiple sections of a project.

Although both include and require serve similar purposes, they work differently and have different uses.

PHP developers of all levels will find this article valuable in learning how to use PHP include and require efficiently.



PHP Include and Require Statements

With the help of PHP include or require statements, you can insert the contents of one PHP file into another PHP file (before it is executed by the server), and then execute the result.

As far as the include and require statements are concerned, they are identical except in the case of failure:

MethodsOverview
requireIf require is executed, a fatal error (E_COMPILE_ERROR) will be thrown and the script will stop
includeIf you include a line, the script will only produce a warning (E_WARNING) and continue running

If you would like execution to continue and the output to be shown to the user even if there is no include file available, then you should use the include statement.

Use the require statement whenever you need to include a key file as part of a framework, CMS, or a complex PHP program. It will protect your application’s integrity and security if one of the key files is missing accidentally.

You can create one header, one footer, and one menu file for every single web page that you intend to create.

You will then only need to update the header file if the header needs to be updated from time to time.

Syntax

include 'filename';

or

require 'filename';

PHP Include Examples

Let’s say we have a standard header file named “headers.php” that looks similar to what is shown below:

<?php
echo "Header file ";
?>

A header file can be included on a page by using the include statement as follows:

Example: 

<html> <body> <?php include 'headers.php';?> <h1>This is my home page!</h1> <p>This is the first paragraph of the home page</p> </body> </html>

The above example illustrates the basics of PHP include statement.

Now we will use the HTML component to include in PHP files.

We will save HTML header and footer tags into a different files and call it on the home page:

‘header.php’ includes:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Title name</title>
</head>
<body>

footer.php’ includes:

</body>
</html>

Now including these files in home.php looks like:

Example: 

<?php include 'header.php';?><h1>This is my Home Page!</h1> <p>This is the first paragraph of the home page</p><?php include 'footer.php';?>

Let’s suppose we have a standard menu file called “menu.php ”:

<?php
echo '<a href="home.php">Home</a> –
<a href="aboutus.php">About Us</a> –
<a href="services.php">Services</a> –
<a href="contactus.php">Contact Us</a>';
?>
This menu file should be used by all pages on the Web site in order to make navigation easier.

The following is an example of how it can be achieved (we are using an element as it makes it easier to style the menu with Tailwind CSS later on):

Example: 

<html> <body> <div class="navigation"><?php include 'menu.php';?></div> <h1>This is my home page!</h1> <p>This is the first paragraph of the home page.</p> </body> </html>

In below example, we will assume we have a file called user.php, which contains the following variables:

<?php
$first_name='Bill';
$last_name='Gates';
?>

By including the user.php file, we will be able to use the variables that have been stored in the user.php file:

Example: 

<html> <body> <h1>This is my home page!</h1> <?php include 'user.php'; echo "$first_name $last_name is the Co-Founder of Microsoft";?> </body> </html>

PHP Require

PHP require statement can also be used to include a file from a specific location.
However, there is one particularly important difference between include and require.
The script will continue to execute if a file cannot be found by PHP when the script is made with the include statement:

Example: 

<html> <body><h1>This is my Home Page!</h1> <?php include 'file_not_exist.php'; echo "$first_name $last_name is the Co-Founder of Microsoft.";?></body> </html>

For instance, if we use the require statement in the same example, we will not be able to execute the echo statement.

This is because the script execution will die after the require statement returns a fatal error:

Example: 

<html> <body><h1>This is my Home Page!</h1> <?php require 'file_not_exist.php'; echo "$first_name $last_name is the Co-Founder of Microsoft.";?></body> </html>

PHP Include vs Required

The require method is used when an application needs a file in order to function.

PHP include method should be used when a file isn’t required and the application should continue running without the file being found when it’s not available.

Lets merge all the example above in a single example with header footers and user file using include statement:

Example: 

<?php include 'header.php';?><div class="navigation"><?php include 'menu.php';?></div> <h1>This is my Home Page!</h1> <p>This is the first paragraph of the home page</p><?php include 'user.php'; echo "$first_name $last_name is the Co-Founder of Microsoft.";?><?php include 'footer.php';?>

Example Explanation

Above example demonstrates how to use the include function to include external files in a web page.
Here’s how the code works:
  • First we have used PHP include method to include an external file called header.php. This file likely contains HTML code that should be displayed at the top of the page (e.g. a logo or a navigation bar).
  • Then we create a <div> element with the class navigation, and uses the include again to include an external file called menu.php. This file likely contains the code for a navigation menu that should be displayed inside the <div>.
  • The third line of above example creates an <h1> element and some text that says “This is my Home Page!” This is the main content of the page.
  • The fourth line of example creates a <p> element with some additional text for the page.
  • The fifth line of example uses the include function again to include an external file called user.ph“. This file likely contains PHP code that sets the variables $first_name and $last_name to the names of the page’s author or owner.
  • Then we have used the variables $first_name and $last_name to output a sentence that says who the co-founder of Microsoft is.
  • Finally PHP include method is used to include an external file called footer.php. This file likely contains HTML code that should be displayed at the bottom of the page (e.g. copyright information or a link to the site’s privacy policy).

Above example demonstrates how to use the include method in PHP to modularize a web page into smaller, reusable components. By separating different parts of the page into separate files, you can make it easier to manage and maintain the code.

In addition, You can avoid duplicating the same code across multiple pages.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *