Course Banner


File Include

Consider the scenario. We have a simple website in which there are 5 pages. Header, footer, and sidebar of all pages have same design. Only the center content are different. In this case rather than creating whole page for each page create one master page say index.php. Then we create separate pages for header. Footer and sidebar and include these all the pages. The benefit is that, if any of the pages of header or sidebar require changes, we can change at one place and it will reflect at all the pages.

So, how we can do that in PHP? Fortunately, PHP provides few functions through which it’s possible. There are include() and require() functions through which we can include the files.

So, what’s the difference between include and require?

There are following difference between the two.

  • ● If file is included but not found then require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • ● If file is included but not found then include will only produce a warning (E_WARNING) and the script will continue
So, when you want to include any file and even if that file is not found but still you want to execute the code, you can go for include() function. On the other hand if you want to stop the execution if the file is not found then it’s better to use require () function. Example : This is our complete index.php code :

< !DOCTYPE html>
< html>
< head>
< title> Welcome to e-commerce website< /title>
< style>
body{margin-top:0px;}
main{width:1024px; margin:auto; border:0px solid red; height:700px;}
header{height:100px; background:black;color:white;}
#header_left{width:300px;float:left;padding-left:20px;}
#header_right{float:right; width:400px;padding-top:30px;}
#content{border:1px solid blue;height:600px;}
aside{width:250px;border:1px dashed blue;height:600px;float:left;}
#right_content{width:750px;float:right;border:1px dashed blue;}
#menu_left ul{ padding:20px;}
#menu_left ul li{font-weight:bold;font-size:18px;list-style:none;padding:0px;}
#menu_left ul li ul li{font-weight:normal;border-bottom:1px dotted blue;}
< /style>
< /head>
< body>
< main>
< header>
< div id="header_left">< h1>E-Commerce Website< /h1>< /div>
< div id="header_right">Welcome Admin | Logout< /div>
< /header>
< div id="content">
< aside>
< h3>Main Menu< /h3>
< div id="menu_left">
< ul>
< li>Categories
< ul>
< li>Add Categories< /li>
< li>View Categories< /li>
< /ul>
< /li>
< li>Products
< ul>
< li>Add Product< /li>
< li>View Product< /li>
< /ul>
< /li>
< li>Orders
< ul>
< li>View Orders< /li>
< /ul>
< /li>
< /ul>
< /ul>
< /aside>
< div id="right_content">
< h1>Welcome Admin< /h1>
< /div>
< /div>
< /main>
< /body>
< /html>

And the output is :

Now we will separate header.php.and sidebar.php

Below is the code:

header.php

< header>
< div id="header_left">< h1>E-Commerce Website< /h1>< /div>
< div id="header_right">Welcome Admin | Logout< /div>
< /header>

sidebar.php

< aside>
< h3>Main Menu< /h3>
< div id="menu_left">
< ul>
< li>Categories
< ul>
< li>Add Categories< /li>
< li>View Categories< /li>
< /ul>
< /li>
< li>Products
< ul>
< li>Add Product< /li>
< li>View Product< /li>
< /ul>
< /li>
< li>Orders
< ul>
< li>View Orders< /li>
< /ul>
< /li>
< /ul>
< /ul>
< /aside>

And now modified index.php

< !DOCTYPE html>
< html>
< head>
< title> Welcome to e-commerce website< /title>
< style>
body{margin-top:0px;}
main{width:1024px; margin:auto; border:0px solid red; height:700px;}
header{height:100px; background:black;color:white;}
#header_left{width:300px;float:left;padding-left:20px;}
#header_right{float:right; width:400px;padding-top:30px;}
#content{border:1px solid blue;height:600px;}
aside{width:250px;border:1px dashed blue;height:600px;float:left;}
#right_content{width:750px;float:right;border:1px dashed blue;}
#menu_left ul{ padding:20px;}
#menu_left ul li{font-weight:bold;font-size:18px;list-style:none;padding:0px;}
#menu_left ul li ul li{font-weight:normal;border-bottom:1px dotted blue;}
< /style>
< /head>
< body>
< main>

<?php
  1. include "header.php";
  2. ?>

< div id="content">

<?php
  1. require "sidebar.php";
  2. ?>

< div id="right_content">
< h1>Welcome Admin< /h1>
< /div>
< /div>
< /main>
< /body>
< /html>

And the output will be same:

Now, if we want to create one more page called categories.php we just need to save as index.php to categories.php

And change only central section and not the whole page.

Example :

categories.php

< !DOCTYPE html>
< html>
< head>
< title> Welcome to e-commerce website< /title>
< style>
body{margin-top:0px;}
main{width:1024px; margin:auto; border:0px solid red; height:700px;}
header{height:100px; background:black;color:white;}
#header_left{width:300px;float:left;padding-left:20px;}
#header_right{float:right; width:400px;padding-top:30px;}
#content{border:1px solid blue;height:600px;}
aside{width:250px;border:1px dashed blue;height:600px;float:left;}
#right_content{width:750px;float:right;border:1px dashed blue;}
#menu_left ul{ padding:20px;}
#menu_left ul li{font-weight:bold;font-size:18px;list-style:none;padding:0px;}
#menu_left ul li ul li{font-weight:normal;border-bottom:1px dotted blue;}
< /style>
< /head>
< body>
< main>

<?php
  1. include "header.php";
  2. ?>

< div id="content">

<?php
  1. require "sidebar.php";
  2. ?>

< div id="right_content">
< h1>This is categories pagge< /h1>
< /div>
< /div>
< /main>
< /body>
< /html>

And the output will be :

As you can see we had to just write code inside center and rest of the design is same.

What is difference between require and require_once ?

If same file say db.php is included in the top of the file and again you included in the middle of the page. If you write require then it will throw error. Saying the file is already included. But on the case of require once. If more than one time file is called, it will skip the second one and will now show any error.

Example :

<?php
  1. functioncall_me() {
  2.       echo"Here is some message";
  3. }
  4. ?>

We have created a file called call_me.php and put the above code. Now I will include this file in our header section as well as sidebar(may be mistakenly). First we will use require() function.

Header.php

< header>
< div id="header_left">< h1>E-Commerce Website< /h1>< /div>
< div id="header_right">Welcome Admin | Logout< /div>
< /header>

<?php
  1. require(“call_me.php”);
  2. ?>

And then include same file in sidear.php

< aside>

<?php
  1. require(“call_me.php”);
  2. ?>

< h3>Main Menu< /h3>
< div id="menu_left">
< ul>
< li>Categories
< ul>
< li>Add Categories< /li>
< li>View Categories< /li>
< /ul>
< /li>
< li>Products
< ul>
< li>Add Product< /li>
< li>View Product< /li>
< /ul>
< /li>
< li>Orders
< ul>
< li>View Orders< /li>
< /ul>
< /li>
< /ul>
< /ul>
< /aside>

You will get above error.

Change require to require_once() both the places (header.php and sidebar.php) and you will not get any error.

<?php
  1. require_once('call_me.php');
  2. ?>

Same applies to include() and include_once()