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’.

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.