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:
|require||If require is executed, a fatal error (E_COMPILE_ERROR) will be thrown and the script will stop|
|include||If 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.
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:
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:
<!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>
Now including these files in home.php looks like:
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>'; ?>
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):
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:
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:
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:
- 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.
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.