PHP Include And Require Files. And Everything You Need To Know About It
Posted by TotalDC
Lets continue our PHP tutorial series by learning everything you need to know about PHP include and require files.
How PHP Include And Require Files Works
The include() and require() statements allow you to include the code contained in a PHP file to another PHP file. Including a file produces the same result as copying the script from the file and pasting it into the location where you need it.
By doing so you can save a lot of time and work – just store a block of code in a separate file and include it wherever you want using the include() and require() statements instead of typing the entire block of code multiple times. Example would be including the header, footer and menu file into all the pages of a website.
The basic syntax of the include() and require() statement looks like this:
include("path/to/filename"); -Or- include "path/to/filename";
require("path/to/filename"); -Or- require "path/to/filename";
The following example shows you how to include header, footer and menu when they are stored in separate files. Using this method you can update all pages of the website at once by making changes to one file, this saves a lot of time.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simply Web Stuff</title>
</head>
<body>
<?php include "header.php"; ?>
<?php include "menu.php"; ?>
<h1>Welcome to the website</h1>
<p>Here you will find lots of useful information.</p>
<?php include "footer.php"; ?>
</body>
</html>
Difference Between Include And Require Statements
Yes, you can include files by using include() or require(). Typically they both operates the same. But here is the difference – include() statement will generate a PHP warning but allow code execution if the file you included can’t be found, while require() statement gives you a fatal error and stops the execution of your code.
<?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 the website</h1>
<p>Here you will find lots of useful information.</p>
<?php include "footer.php"; ?>
</body>
</html>
The include_once and require_once Statements
PHP provides include_once() and require_once() statements to prevent conflicts that may be caused if you would include the same file more than once to your code using include() or require() statements.
The include_once() and require_once() statements will only include the file once even if you try to do that the second time. To better understand how it works let’s look at the example:
<?php
function multiplySelf($var){
$var *= $var; // multiply variable by itself
print $var;
}
?>
Here’s is the PHP code within which we have included the ‘my_functions.php’ file:
<?php
// Including file
require "my_functions.php";
// Calling the function
multiplySelf(2); // Result: 4
print "<br>";
// Including file once again
require "my_functions.php";
// Calling the function
multiplySelf(5); // Does not execute
?>
When you run the code above, you will see the error message something like this – “Fatal error: Cannot redeclare multiplySelf()”. This happens because the ‘my_functions.php’ is included twice. This means that the function multiplySelf() is defined twice and that caused PHP to stop the code execution and generate fatal error. Now here is the exact same example but with require_once() function:
<?php
// Including file
require_once "my_functions.php";
// Calling the function
multiplySelf(2); // Result: 4
print "<br>";
// Including file once again
require_once "my_functions.php";
// Calling the function
multiplySelf(5); // Result: 25
?>
As you can see, by using require_once instead of require, your code runs as expected.
Leave a Reply