PHP Tutorials: include() and require()

We studied the scope of the variable in our previous tutorials and I am sure you had gone through it. If not then click the link All about variables and check it out. We learned a lot till now and it’s time to move with some advanced concepts.

Till now we know how to declare functions and variables. But, there are a lot of the PHP libraries available in the open-source market which can help you solve your problem. But using the library can be challenging if you don’t know how to call the functions of the library.

the include() and the require() function are used to include code of one file into another. include() and require() is very similar to copy and paste code of a file into another. You can relate it to functions. Functions get defined once and can be called multiple times.

Include and require statements are used when you recording in a structured manner. With structured manner I mean to say that you are going to reuse the code in multiple files. It can be similar header file, footer file, or database connection file.

Before moving further let us see the syntax of Include and require.

<?php 
include("path/to/filename"); -Or- include "path/to/filename";
require("path/to/filename"); -Or- require "path/to/filename";
?>

Include and require statements works very similar to the echo and print. You can ignore the parenthesis or use them.

Let’s take an example where you can use a common header throughout the website. When making a change to the header file, the changes will reflect in the entire website without opening any other file.

<!-- index.php -->
<!DOCTYPE html>
<html lang="en">
<head>
    <title>LakBlog.com</title>
</head>
<body>
<?php include "header.php"; ?>
<?php include "sidebar.php"; ?>
    <h1>Welcome to LakBlog.com!</h1>
    <p>Demonstrating the include and require.</p>
<?php include "footer.php"; ?>
</body>
</html>
<?php
//header.php
?>
<header>
     <h1>Header Goes Here</h1>
</header>
<aside>
     <h1>Sidebar Goes Here</h1>
</aside>
<?php
//footer.php
?>
<footer>
     <h1>Footer Goes Here</h1>
</footer>

How include and require are different?

Although both functions work very similarly and are not different in a big manner. The only difference between both the functions is the execution of PHP program. With the include statement, if the file which was included is missing, the PHP program keep executing and just give a warning. But in case of require a PHP program gives a fatal error and stop the program if the file is missing.

The require statement is the recommended when you are including a library or any configuration file that is required to execute the program. Include can be used when the file is not important.

<?php require "config.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title><?php site_title(); ?></title>
</head>
<body>
<?php include "header.php"; ?>
<?php include "sidebar.php"; ?>
    <h1>Welcome to <?php site_title(); ?>!</h1>
    <p>Demonstrating the include and require.</p>
<?php include "footer.php"; ?>
</body>
</html>

Leave a Reply

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