In this article, you will learn about PHP includes and how to evaluate files. This article is a continuation of the previous topic, entitled fopen PHP.
In PHP, include files or (require
) is a statement that takes all the (text/code/markup
) which exists in the specific file and copied into the file which uses the include statement.
What is PHP includes?
The include() is a statement that allows you to include the contained code into a PHP file within another file. Which includes a file that produces the same results when copying the script from the specified file and pasted on the location where it was called.
In addition, including a file can save you a lot of time in your work. In order to use the include()
you just need to store a (block of code
) into a separate file and include it or call it wherever you want using the include()
and require()
statements instead of manually typing the entire block of code many times.
The best example includes the header, menu file, sidebar, and navbar files within the entire pages of a system or a website.
Syntax of include() and require statements.
include("path/to/filename"); -Or- include "path/to/filename";
require("path/to/filename"); -Or- require "path/to/filename";
The following example below will show you on how to include the header, menu and footer that is stored in a separate (header.php, menu.php, and footer.php
) files separately in the entire pages of the system or website.
Furthermore, with the use of this technique you can easily update all the web pages at once by making all the changes in just one file, this saves a lot of time in repetitive work.
For example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tutorial Republic</title>
</head>
<body>
<?php include "header.php"; ?>
<?php include "menu.php"; ?>
<h1>Welcome to ITSOURCECODE.COM!</h1>
<p>Here you will find lots of useful information.</p>
<?php include "footer.php"; ?>
</body>
</html>
Advantages of Include() in PHP
- Reusability of the code – we can easily reuse the HTML code in some scripts in PHP code with the help of include() and require() statement to build.
- Very simple to edit – There are some instances in which we want to update some scripts in a different webpage, we can easily modify the file on each webpage rather than editing each source file manually.
What is difference between require_once(), require(), and include()?
The main difference of the three statements is that the require_once()
simply means that it will just need it and only require once. The require()
simply means it needs it and the include()
simply means that it will just include a file but it does not need to be continuous.
In order for you to understand it easily I will further discuss it below and provide a detailed explanation as well as program examples.
Difference between include and require statements in PHP
There might be instances that we might be thinking if we can include a file with the use of include() statement then why we a require() statement if we can use the include. There is a little bit difference between the two statements but it operates the same.
In addition, the only difference is the include()
statement will only generate a warning and the script also allows a script to continue execution if the file is included and cannot be found. While the require()
statement will automatically generate a (fatal error
) and stops all the script execution.
For example:
<?php require "my_variables.php"; ?>
<?php require "my_functions.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php displayTitle($home_page); ?></title>
</head>
<body>
<?php include "header.php"; ?>
<?php include "menu.php"; ?>
<h1>Welcome to ITSOURCECOE.COM</h1>
<p>Here you will find lots of useful information and free source code.</p>
<?php include "footer.php"; ?>
</body>
</html>
The include_once and require_once statements in PHP
There might be instances that we accidentally include the same file commonly the (classes or functions files
) more than once within the entire code with the use include or require statements, which may cause conflicts.
In order to prevent this kind of scenario. The PHP provides require_once and include_once statements. These two statements behave in the same manner as required and include statements with just only one exception.
The require_once
and include_once
statements will include only the file once even though we call the include for the second time. Once the specific target file has already been included in the previous statement, the file will be not included again. In order for you to understand it correctly on how it works, let us check out an example program. Supposedly we have a files name (my_functions.php
).
For example:
<?php
function multiplySelf($var){
$var *= $var; // multiply variable by itself
echo $var;
}
?>
The following program below is a PHP script within the (my_functions.php
) file.
For example:
<?php
// Including file
require "my_functions.php";
// Calling the function
multiplySelf(2); // Output: 4
echo "<br>";
// Including file once again
require "my_functions.php";
// Calling the function
multiplySelf(5); // Doesn't execute
?>
Once you run the script above, you will see an error message saying like this ‘Fatal error: Cannot redeclare multiplySelf()
‘. This error occurs because the ‘my_functions.php
‘ included it twice, It simply means that the function multiplySelf()
is defined twice, which caused the script execution to stop and generate a fatal error. Now we will rewrite the program example above with the use of require_once
.
<?php
// Including file
require_once "my_functions.php";
// Calling the function
multiplySelf(3); // Output: 6
echo "<br>";
// Including file once again
require_once "my_functions.php";
// Calling the function
multiplySelf(4); // Output: 20
?>
Include in PHP
The include is a keyword which use to include one file into another PHP. While including all the content of the file that has been included and it will be displayed in the main file.
For example:
Page1.php
<?php
echo "<p>welcome to ITSOURCECODE.COM</p>";
?>
Main.php
<html>
<body>
<h1>Welcome to home page of itsourcecode!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'Page1.php';?>
</body>
</html>
Require in PHP
The require function is similar to an include, which is also used to include files. The only difference of the two is that the file cannot be found and also prevents the script from running, while the includes are not.
In addition, the require()
function can copy all the text from a file given into the uses of the include function. The require() function will produce a fatal error and automatically stops the execution scripts when there is a problem when loading the file.
A part from how they were treated an error conditions, include and require() are identical. Since the entire script does not execute once the files were missing or misnamed, the function require will recommend over an include function.
For example:
Menu1.html:
<html>
<body>
<ahref="http://www.pythonforfree.com">Google</a> |
<ahref="//www.itsourcecode.com">Yahoo</a> |
<ahref="http://www.sourcecodehero.com">Maps</a> |
<ahref="http://www.pies.com">Tutorial</a>
</body>
</html>
Main.html:
<html>
<body>
<h1>welcome</h1>
<?php require("menu1.html"); ?>
</body>
</html>
PHP include vs require
In PHP, the require()
statement works the same as the include() statement. The difference of the two is that the include() statement generates an (alert) and allows script execution to proceed once the file to be included is not found. In addition, the require statement also generates a fatal error and stops the PHP script.
Related Articles
- PHP PDO Fetch (With Detailed Explanation)
- PHP Ucwords (With Advanced Examples)
- PHP Session Function (Start and Destroy)
- PHP Cookies with Best Example
- File Inclusion in PHP (With Program Examples)
Summary
This article discussed PHP includes with detailed explanation and example program. And also tackles Advantages of Include(), what is difference between require_once(), require(), and include(), difference between include and require statements, the include_once and require_once statements, and include vs require.
I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials that could help you a lot.