PHP File Inclusion

PHP File Inclusion

Using php file inclusion you can include the content of a PHP file into another PHP file before exicution of the server. There are two PHP functions which can be used to included one PHP file into another PHP file.

  • The include() Function
  • The include_once() Function
  • The require() Function
  • The require _once() Function

This is a strong point of PHP language which helps you in creating seprate functions, headers, footers files that can be include into multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file and reduce your time and effort

The include() Function

The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.
Assume you want to create a common navigation for your website. Then create a file menu.php with the following content.

<a href="http://www.phpkida.com/ ">Home</a> |
<a href="http://www. phpkida.com/php">PHP</a> | 
<a href="http://www. phpkida.com/ajax">AJAX</a> | 
<a href="http://www. phpkida.com/perl">HTML</a>

Now here i am creating only single page but you can create many pages as you like and include this file into your pages. For example now your index.php file can have following content.

The PHP include_once()

The include_once() statement can be used to include a php file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.

Example PHP require_once()

<html>
<body>
<?php require_once("menu.php"); ?>
<p>This is an simple example to show how to include PHP file using include_once()!</p>
<?php require_once("menu.php"); ?>
<p>One more time include PHP file using include_once() in same page!</p>
</body>
</html>

The require() Function

The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and stop the execution of the script.
So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
Now here i am creating only single page but you can create many pages as you like and include this file into your pages using require() function. For example now your index.php file can have following content.

<html>
<body>
<?php require("menu.php"); ?>
<p>This is an simple example to show how to include PHP file using require!</p>
</body>
</html>

PHP require_once()

require_once() statement can be used to include a php file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.

Example PHP require_once()

<html>
<body>
<?php require_once("menu.php"); ?>
<p>This is an simple example to show how to include PHP file using require_once()!</p>
<?php require_once("menu.php"); ?>
<p>One more time include PHP file using require_once() in same page!</p>
</body>
</html>

NOTE: You may get simple warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.

What’s the difference between include and require.

Both function the same but there is a significant difference.First up include or require are not a functions, they are constructs. It is therefore not necessary to call them using parentheses like include(‘php-file.php’); instead it is preferred to use include ‘php-file.php’.

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.