Follow us on Facebook
Faucibus toroot menuts
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.
There are following difference between the two.
< !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>
Now we will separate header.php.and sidebar.php
Below is the code:
< header>
< div id="header_left">< h1>E-Commerce Website< /h1>< /div>
< div id="header_right">Welcome Admin | Logout< /div>
< /header>
< 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>
< !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>
< div id="content">
< div id="right_content">
< h1>Welcome Admin< /h1>
< /div>
< /div>
< /main>
< /body>
< /html>
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.
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>
< div id="content">
< div id="right_content">
< h1>This is categories pagge< /h1>
< /div>
< /div>
< /main>
< /body>
< /html>
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.
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>
< div id="header_left">< h1>E-Commerce Website< /h1>< /div>
< div id="header_right">Welcome Admin | Logout< /div>
< /header>
And then include same file in sidear.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>
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.
Same applies to include() and include_once()