Information Technology Reference
In-Depth Information
Listing 4-1. Using a PHP Include Statement We Will Pull In Extra Code to be Included On Our Page
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Super Cool Web Application</title>
</head>
<body>
<header>Super Awesome Header</header>
<article>
<header>Article Header</header>
<p>Fusce luctus accumsan odio. Cras vel sodales mi. Suspendisse et arcu quis magna
feugiat ultrices sit amet non erat. Etiam malesuada dui venenatis eros gravida aliquet.
Aliquam erat volutpat. Nullam dapibus cursus ultricies. Suspendisse congue accumsan
purus non scelerisque. Phasellus ut sapien libero, vel luctus velit.</p>
</article>
<!-- This is where we use PHP to include another file into our document -->
<?php include('footer.php'); ?>
</body>
</html>
If you look toward the bottom of that listing you will notice that I am pulling in the page
footer.php (see Listing 4-2). I am doing this by wrapping my include function in the
<?php ?> tags. This tells your page to execute PHP code if your server supports it, and
in this code is looking for the footer document that is in the same relative path as the
document listed previously. If you are using the Uniform Server local development server
that we discussed earlier in the topic, then this should work like a charm. Otherwise we
may need to specify a relative path; for example, if I put all of my “included” files into a
directory named “includes,” my code may read: include('includes/footer.php') . We'll
talk about directory structure in a moment, so once you're done here you'll know how
the relative path will look to your files.
Now that we know what it looks like to include a file in your document using PHP, we
are going to take a look at what the innards of that PHP file look like, so you can see
how simple it is to structure your code in an easy, reusable fashion.
Listing 4-2. A Look Inside the footer.php File
<footer>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Privacy Policy</a></li>
<li><a href="#">Terms and Conditions</a></li>
</ul>
<p>&copy; 2011 Super Awesome Site, Inc. All rights reserved.</p>
</footer>
As you can see here, the code in this document is pretty basic. We have an unordered
list that is floated to the right of the document and some copyright text that will float to
the left of it. We will just imagine that somewhere off in the distance there is a stylesheet
created for this document that makes everything pretty.
 
Search WWH ::




Custom Search