To start, let's create a new PHP page for the navigation. I'll call mine navigation.php (what an original name, lol). In this file, I'll use the following HTML to create a set of links:
<b>Sitely</b><br>
<li><a href="http://www.neo-reality.org/index.php">Home</a><br>
<li><a href="http://www.neo-reality.org/about.php">About Us</a><br>
<li><a href="http://www.neo-reality.org/rules.php">Rules</a><br>
<li><a href="http://www.neo-reality.org/contact.php">Contact Us</a><br>
The links above all end in .php, but you don't necessarily need to have links which end in .php - other file extensions will still work. Now on your index.php page, you can display your navigation.php page by using the following code:
| <?php include("navigation.php"); ?> |
So now when you open up your index.php page you'd see the following display:
Sitely
You can stick the PHP code into a table or div or wherever your layout requires and this will show up exactly as it would if you pasted the HTML code straight into the index page.
But sometimes the problem with includes is that the rest of the php script will continue to function even when the file being included doesn't exist. For example, say I accidently put in a page that didn't exist, we'll call it error404.php:
|
<?php include("error404.php"); echo "The rest of the page is displayed here!"; ?> |
The page would then display out like so:
Warning: main(error404.php): failed to open stream: No such file or directory in /host/yoursite/yourfolder/page.php on line 2
Warning: main(): Failed opening 'error404.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /host/yoursite/yourfolder/page.php on line 2
The rest of the page is displayed here!
Notice how the echo statement is still executed. This is because a warning does not prevent the rest of the PHP script from running. On the other hand, if we did the same example but used the require statement:
|
<?php require("error404.php"); echo "The rest of the page is displayed here!"; ?> |
And you'd see something like this:
Warning: main(error404.php): failed to open stream: No such file or directory in /host/yoursite/yourfolder/page.php on line 2
Fatal error: main(): Failed opening required 'error404.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /host/yoursite/yourfolder/page.php on line 2
This time the echo statement isn't executed because the file error404.php was required for the rest of the script to run.