---
title: "File Inclusion in PHP"  
description: "In PHP you can include the content of one file into another file. This will help in creating function, footers header or elements that can be used in"  
author: "Royce Roy"  
published: 2016-06-09  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11150/file-inclusion-in-php  
category: "php"  
tags: ["php", "file"]  
reading_time: 3 minutes  

---

# File Inclusion in PHP

In PHP you can include the [content of one](https://www.mindstick.com/forum/118/problem-in-display-content-of-one-page-to-another-page) file into another file. This will help in creating function, footers header or elements that can be used in several pages. If there is any changes needed in thousand of files just change included file.

## There are two PHP functions which are used to include the file:

· The include() function

· The require() function

· The require_once function

· The include_once function

Both these functions are used to include the file. Both are same the only [difference between them](https://www.mindstick.com/forum/23342/what-are-jsf-servlet-and-jsp-and-what-is-difference-between-them) is that if there is any problem in loading the file the require() function generates a [fatal error](https://answers.mindstick.com/qa/94996/how-do-i-fix-a-fatal-error-in-windows-10) and terminates the program.

##### The include() function

This function takes all the content into another file which has**include()**function. If there is any problem while loading a file than this function only generates the warning but not halt the program and continue with the execution.\

**Note:**include will only produce a warning (E_WARNING) and continue with the execution.

**Step 1:** Create a file text.php which will be included.

```
XML = EXtensible Markup Language</br>PHP = PHP Hypertext Preprocessor</br>CSS = Cascading Style Sheets</br>HTML = Hyper Text Markup Language</br>SQL = Structured Query Language</br>AJAX = Asynchronous JavaScript and XML</br>SVG = Scalable Vector Graphics</br>
```

**Step 2:** Create a file include.php which will include the content of text.php

```
<?phpinclude("text.php");echo '<b> file(text.php) is included in include.php file.</b>'?>
```

**Output:**

[XML](https://www.mindstick.com/forum/145511/please-explain-json-vs-xml) = EXtensible [Markup Language](https://www.mindstick.com/interview/234/what-is-markup-language)

PHP = PHP Hypertext Preprocessor

CSS = Cascading [Style Sheets](https://www.mindstick.com/forum/506/why-do-we-use-style-sheets)

HTML = Hyper Text Markup Language

SQL = [Structured Query Language](https://www.mindstick.com/blog/11168/introduction-to-sql)

AJAX = [Asynchronous](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) JavaScript and XML

SVG = Scalable Vector Graphics

file(text.php) is included in include.php file.

**Note:** The include_once() function: The include_once() have the similar behavior to include() function, except that if the file is already included, it will not include the file again. [As the name](https://www.mindstick.com/forum/34352/the-term-update-database-is-not-recognized-as-the-name-of-a-cmdlet-function) suggests, the file will be included just once.

##### The require() function

This function takes all the content into another file which has **require()** function. If there is any problem while loading a file than this function generates the fatal error and terminates the execution of program. It is recommended to use require() function instead of include() function because program should not continue its execution if there is a missing file.

**Note:** require() will produce a fatal **error (E_COMPILE_ERROR)** and stop the program.

You can try the above example using require() function it will generate the same result. Let’s try to include the file which does not exist.

```
<?phpinclude("t.php");echo '<b> This example is used to show how include function had included the wrong PHP file which not even exist.</b>'
?>
```

##### Output:

Warning: include(t.php): failed to open stream: No such file or directory

his example is used to show how include function had included the wrong PHP file which not even exist. // continue its execution

**Using require() function**

```
<?phprequire ("t.php");echo '<b> This example is used to show how include function had included the wrong PHP file which not even exist.</b>'
?>
```

\

## Output:

It will show a fatal error and halt the program.

**Note****:** The **require_once()** function: The require_once() have the similar behavior to require() function, except that if the file is already included, it will not include the file again. As the name suggests, the file will be included just once.

---

Original Source: https://www.mindstick.com/blog/11150/file-inclusion-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
