Now that we have included a very small PHP script onto our page let's build it up slightly more using includes. Includes are very handy when using sites that will have one navigation spread across 100+ pages, using includes allows you to have the navigation only written out once and included dynamically onto all pages you want it.
Here's how it's done, take your normal page;
index.php
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Example</title> </head> <body> <div id="navigation"> <ul> <li><a href="#">link 1</a></li> <li><a href="#">link 2</a></li> <li><a href="#">link 3</a></li> <li><a href="#">link 4</a></li> <li><a href="#">link 5</a></li> </ul> </div> <div id="content"> <p>page content will appear here</p> </div> </body> </html> |
Here's our normal page, now without PHP you would have to copy the navigation manually to all pages, with PHP there's no need!
Take out the navigation part of the code;
| <div id="navigation">
<ul> <li><a href="#">link 1</a></li> <li><a href="#">link 2</a></li> <li><a href="#">link 3</a></li> <li><a href="#">link 4</a></li> <li><a href="#">link 5</a></li> </ul> </div> |
And place it into notepad, saving the file as "navigation.php". Next we need to edit the original index.php page to include the navigation inside it. This is done like so;
index.php
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Example</title> </head> <body> <? php include 'navigation.php'; ?> <div id="content"> <p>page content will appear here</p> </div> </body> </html> |
As you can see using PHP we have included the navigation into the page, when viewing the page (on a server) you can see that the navigation appears as it normally would using the original code (non-PHP version). The php include has the added bonus being included on all pages you use the include code and when you edit navigation.php it will change on all the other pages it is included on.






