---
title: "Include and Require function in PHP"  
description: "Before discussing about include () and require () function in PHP first we will described what is Server Side Includes (SSI)? Server Side Includes (SS"  
author: "Anonymous User"  
published: 2011-09-13  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/727/include-and-require-function-in-php  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# Include and Require function in PHP

Before discussing about include () and require () function in PHP first we will described what is [Server Side](https://www.mindstick.com/forum/143/close-popup-window-by-server-side-code) Includes (SSI)? Server Side Includes (SSI) is an [efficient](https://www.mindstick.com/articles/33637/three-tips-for-an-energy-efficient-home-for-2019) way through which we can insert all [content of one](https://www.mindstick.com/forum/118/problem-in-display-content-of-one-page-to-another-page) PHP file to another PHP file before the server executes it, with include() or require() function.\

##### The two functions are identical in every way, except how they handle errors:

- include() generates a warning, but the script will continue [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net)
- require() generates a [fatal error](https://answers.mindstick.com/qa/94996/how-do-i-fix-a-fatal-error-in-windows-10), and the script will stop

These two [functions](https://www.mindstick.com/forum/34086/jquery-callback-functions) are used to create functions, headers, footers, or elements that will be reused on multiple pages.

##### Include () function:

include () function takes all the content in a specified file and includes it in the current file. If an error occurs, include () function generates a warning, but the script will continue execution.

##### Example:

Now let’s assume that we have a [master page](https://www.mindstick.com/forum/649/set-header-and-footer-with-in-master-page), called ‘MasterPage.php’. To include this file into ‘home.php’ page, use include () function.

##### Code of ‘Home.php’ page:

```
   <!DOCTYPEhtml><html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">         <title>PhpInclude File</title>     </head>     <body>         <table id="bodyText">             <tr>                <td>                  <div id ="leftMenu">                    <?php                           //Include all content of Master page                       include ("MasterPage.php");                      ?>                </div>               </td>           <td>            <p>Welcome to Home Page</p>            </td>        </tr>     </table>             </body></html>
```

Here you can use require () function in place of include () function because both are identical in every way, except how they [handle error](https://www.mindstick.com/forum/159014/how-does-rust-handle-error-handling-and-what-are-the-advantages-of-its-approach).

All pages in the [Web site](https://www.mindstick.com/articles/12285/the-finest-web-site-design-trends-you-may-expect-in-2017) should include this master file. Here is how it can be done:

##### Code of ‘MasterPage.php’ page:

```
<a href="Timestamp.php" alt="Timestamp" > Timestamp</a></br> <a href="Welcome.php" alt="Welcome"> Welcome</a></br>   <a href="Helloword.php" alt="Hello"> Hello word</a></br>
```

Now execute the ‘Home.php’ page, following output look like this.

![Include and Require function in PHP](https://www.mindstick.com/mindstickarticle/84636594-7f15-4330-a975-b1040aea56e0/images/bff8fc74-2888-448a-8187-3fd62c0511f0.png)

If you look at the source code of the page above (in a browser), it will look like this:

![Include and Require function in PHP](https://www.mindstick.com/mindstickarticle/84636594-7f15-4330-a975-b1040aea56e0/images/34f6cf19-221e-4203-967b-50affa32c63b.png)

##### require () function :

require () function works same as include function except how they [handle errors](https://www.mindstick.com/forum/159698/how-to-handle-errors-and-exceptions-in-a-dot-net-core-api).

Let’s we have an example, how they handles error.

Example: Include php file with the help of include() function.

```
 <!DOCTYPEhtml><html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">         <title></title>     </head>     <body>         <?php            //  Include wrong file(i.e does not exist)  with include function             include 'Temporary.php'.'</br>';                        //  display a message after including 'Temporary.php' file             echo '</br>'.'Message: Temporary file included';         ?>     </body></html>
```

##### Output:

![Include and Require function in PHP](https://www.mindstick.com/mindstickarticle/84636594-7f15-4330-a975-b1040aea56e0/images/c79a29d6-69a5-4308-8e19-5ed73826b262.png)

Example: Include php file with the help of require () function.

```
<!DOCTYPE html><html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">         <title></title>     </head>     <body>         <?php            //  Include wrong file(i.e does not exist)  with require function             require 'Temporary.php'.'</br>';                        //  display a message after including 'Temporary.php' file             echo '</br>'.'Message: Temporary file included';         ?>     </body></html>
```

##### Output:

![Include and Require function in PHP](https://www.mindstick.com/mindstickarticle/84636594-7f15-4330-a975-b1040aea56e0/images/adb28363-21ca-4aa8-977b-6357ee35a69a.png)

From above two outputs it is clear that If an error occurs (when including php file), include () function generates a warning, but the script will continue execution and message is going to be display but require () generates a fatal error, and the script will stop.

---

Original Source: https://www.mindstick.com/articles/727/include-and-require-function-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
