PHP Include and Require Files

You can include the content of one PHP file to another PHP file to reduce the effort of rewriting the same code again. Including a PHP file to another works the same as copy and paste the content of one file to another.

There are two built-in functions are available in PHP to achieve the same.

  • The include() function
  • The require() function

This is the advantage of using PHP. You can create separate files for header, footer, functions, or elements that can be reused in multiple pages. This will help developers to change the code just in one file instead of changing it on thousands of files. For example, they want to change something in the header section of the website, they can do it by changing only the header file and changes will reflect all the pages which included header file.

The include() Function

The include() function adds all the text of one file to another PHP file that uses the include function. In case of any problem for loading the included file, PHP will give a warning message but will continue the execution of the script.

Example

Suppose, you want to create a website that will include header, menu, body area, and footer. Below is the example of the sample code.

File: inclueexample.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Tutorials Book</title>
</head>
<body>
<?php include "header.php"; ?>
<?php include "menu.php"; ?>
    <h1>Welcome to Tutorials Book!</h1>
    <p>Here you will find tutorials on different topics.</p>
<?php include "footer.php"; ?>
</body>
</html>

Now you have to create 3 different PHP files with the name of “header.php”, “menu.php” and “footer.php”.

header.php

<h3>This is Header Section</h3>

File: menu.php

<h4>This is Menu area</h4>

File: footer.php 

<h3>This is footer area</h3>

Output
php include function example

The require() Function

The require() function copy all the text from the one file to another PHP file which includes the require() function in it. In case of any problem of loading the required file, PHP will give fatal error and halt the execution of the script.

There is no difference between PHP include() and PHP require() function except the exception handling. so, it is recommended to use require() as instead of include() as it is not expected to continue the script incase any file(s) is missing.

Example

You can try the above example just replacing include() with require() and the script will give the same output.

Now, if internationally you change the name of the header.php file as headersec.php in the script like below.

File: requireexample.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Tutorials Book</title>
</head>
<body>
<?php require "headersec.php"; ?>
<?php require "menu.php"; ?>
    <h1>Welcome to Tutorials Book!</h1>
    <p>Here you will find tutorials on different topics.</p>
<?php require "footer.php"; ?>
</body>
</html>

But you must not create headersec.php file this time for experimenting purpose. Now if you try to execute the script, PHP will through fatal error and execution halts.

The include_once and require_once statement

If you accidentally include the same PHP file containing functions and classes in another file more than once it may create conflict. In order to avoid this situation, PHP provides another two built-in functions include_onec() and require_once().

The include_onec() and require_once() include the required file only once, although you tried to include it for the second time. In the second time, PHP checks if the file is already included or not. If included, PHP will simply ignore the statement.

Example

In the following example, we will try to include header.php file twice.

File: include_onceexample.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Tutorials Book</title>
</head>
<body>
<?php include_once "header.php"; ?>
<?php include_once "header.php"; ?>
<?php include_once "menu.php"; ?>
    <h1>Welcome to Tutorials Book!</h1>
    <p>Here you will find tutorials on different topics.</p>
<?php include_once "footer.php"; ?>
</body>
</html>

Output

In this case, the output will same as include() function and the header.php file includes once. And if you replace include_once() with require_once(), it will work the same as require() function except that the header.php will include only once.

Please get connected & share!

Advertisement