PHP Programing language

File Inclusion in PHP
Previous Home Next
adplus-dvertising

File Inclusion

In PHP, the content of php file include to another php file before the server execution. It help to create elements, functions, header or footers etc that can used again and again in multiple pages. If any changes are required then only included file changed this makes easier to the developer.

There are two functions that are used to include a file:
  1. The include() Function
  2. The require() Function

Include() Function: You can include the content of one PHP file into another PHP file. It only produce a warning (E_WARNING) and the script will continue. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Example:

test.php code:

<html>
<head><title>Include() in Php</title>
</head>
<body bgcolor="#FFFF99">
<?php 
include("abc.php");
?>
<p> Other content of the file</p>
</body>
</html>
abc.php code:

<a href="http://r4r.co.in/">Home<br/></a>
<a href="http://r4r.co.in/profile/index.shtml">About Us<br/></a>
<a href="http://r4r.co.in/profile/careers.shtml">Careers<br/></a>
<a href="http://r4r.co.in/profile/contact_us.shtml">Contact Us</a>

Output:

Require() Function: It takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. It will produce a fatal error (E_COMPILE_ERROR) and stop the script.

Example:

test.php code:

<html>
<head><title>Require() in Php</title>
</head>
<body  bgcolor="#FFCCFF">
<?php 
require("abc.php");
?>
<p> Other content of the file</p>
</body>
</html>
abc.php code:

<a href="http://r4r.co.in/">Home<br/></a>
<a href="http://r4r.co.in/profile/index.shtml">About Us<br/></a>
<a href="http://r4r.co.in/profile/careers.shtml">Careers<br/></a>
<a href="http://r4r.co.in/profile/contact_us.shtml">Contact Us</a>

Output:

Previous Home Next