Using PHP include() – Make things easy for yourself!
The PHP include() statement can really help when it comes to editing site layout
Imagine a static site with 40 pages all with the navigation menu hard-coded into all 40 pages. What happens when more links need to be added? ANSWER: Open all 40 files and change the links manually! Not if you use the PHP include statement!
Basic Static PHP file:
<ul>
<li id="nav1">Home</li>
<li id="nav2">Page1</li>
<li id="nav3">Page2</li>
<li id="nav4">Page3</li>
<li id="nav5">Contact</li>
</ul>
<h1>Welcome to my site</h1>
Fig 1.1 – Navigation Hard-Coded into PHP fileIf you used the same layout structure as Fig 1.1 for every page in your site then any change to the navigation bar will need to be changed across EVERY page
The PHP include statement allows you to put the navigation menu into a separate file such as Fig 1.2 below:
<ul>
<li id="nav1">Home</li>
<li id="nav2">Page1</li>
<li id="nav3">Page2</li>
<li id="nav4">Page3</li>
<li id="nav5">Contact</li>
</ul>
Fig 1.2 – navigation.phpYou then ‘include’ the navigation.php in any page where you want the navigation menu to appear by using this very small piece of PHP code:
Fig 1.3 – The PHP include statementPHP include statement implementation:
<h1>Welcome to my site</h1>
Fig 1.4 – How to implement the PHP include()You could put a lot more than just the navigation menu into a separate ‘include’ file.
PHP include allows you to split your site into much smaller and manageable chunks. It is also a form of ‘future-proofing’ your site, meaning that it would be much easier to change your site design when necessary.
Look at the possibilities:
PHP Include () - Possibilities
A ‘real-life’ example from of the sites in my portfolio where I have used the PHP include statement a lot:
<img src="images/home2.jpg" alt="" />
Welcome to *****
Real Life example of the include statement in action!
discuss this topic to forum
