Includes
Admittedly, using PHP to print the message "Hello World" isn't a particularly useful application for PHP. We can accomplish the same thing by just writing the message in our HTML. However, since you now understand the basics of PHP, we can move on to using PHP for something useful.
In this section, we will use the PHP include function to access a header and footer saved in independent files and add them to the beginning and end of our PHP file, respectively.
Creating the Header and Footer
When creating large sites, developers may sometimes find themselves putting the same information in a number of files. This often happens when a site has a header, footer, or even a menu that is the same on every page.
If this information could be stored in a single file, and then accessed by the rest of the pages that require it, then site wide changes to this information become incredibly easy and fast to implement. All a developer would have to do is update the file that is being called upon by the rest of the files in the site.
With PHP, we can do this quite easily.
- The first step is to create the header.
- Assuming all of your HTML documents use the same doctype, it would be logical to include this HTML in our header file as well.
- In this example header, the site's menu is at the top of the site, so we'll include that in our header file as well.
- Here is an example header:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<head> - <title>Dr. Giggle Magee's Website</title>
- </head>
<body> - <h1>PHP Include Example</h1>
<p><strong>Navigation</strong></p>
<ul>
- <li><a href="http://techrangers.ucf.edu">Techranger Website</a></li>
<li><a href="http://php.net/">PHP.net</a></li>
<hr /> - <li><a href="http://techrangers.ucf.edu">Techranger Website</a></li>
- Save your header file as header.php and upload it to your Pegasus account.
- Next, we want to create a footer. In the footer, we want to put a paragraph saying who the page is maintained by and also our close body tag and close html tag.
- Here is an example footer:
- <hr />
<p style="text-align: center;">This page is maintained by Dr. Giggles Magee and evil smurfs.</p>
</html>- <hr />
- Save your footer file as footer.php and upload it to your Pegasus account.
Including the Header and Footer
Now that we have a header and footer, we are ready to include them in a document. We'll go ahead and add them to the top and bottom of the following content using the PHP include function.
- Create a file with the following content in it:
<p>
I, Dr. Giggles Magee, am an evil scientist bent on destroying the world.
</p>
<p>
I lead an army of smurf warriors, who, at my request, sing songs to me as I sleep. They also bake cookies for me on a regular basis, but that isn't important.
</p>
<p>
I really like cookies.
</p>
- Now, at the top of the document, we will open a PHP block using <?php, and then call the include function and provide it with the name of our header file. For example, "include("header.php");" Then, we close our PHP block with ?>. At the bottom of the file, we will do the same thing, except we will pass footer.php to the include function. Here is the final result:
- <?php
- include("header.php");
<p>
I, Dr. Giggles Magee, am an evil scientist bent on destroying the world.
</p>
<p>
I lead an army of smurf warriors, who, at my request, sing songs to me as I sleep. They also bake cookies for me on a regular basis, but that isn't important.
</p>
<p>
I really like cookies.
</p>
<?php
- include("footer.php");
- Save the file as includeexample.php and upload it to your Pegasus account.
- Click here to view our example.
Amending a Problem with this Method
One disadvantage to using this method is that every document using this header is going to have the same title, that is, the text inside the HTML title tags (<title>...</title>), is going to be the same. As it is important to have descriptive titles, we need to find a way to fix this problem.
Don't worry though, be confident with the knowledge you have gained so far, and we will be able to work out a solution in no time.
- If you've closed includeexample.php, open it back up.
- In the first PHP block, we want to create a variable to hold our title. We can do this with the same method we learned earlier. Add the following assignment before the include function:
- $title = "All About Dr. Giggles Magee";
- Save the file as includexample1.php and upload it to your Pegasus account.
- Open header.php
- We could simply open a PHP block inside our HTML title tags and then use echo($title); Unfortunately, this would mean that we would have to have a title variable on every single page of our site. In many cases this would be unnecessary because sometimes a simple generic title will suffice. Instead of using this simpler method, we can make our header.php file use a default title just in case we don't have a $title in the document we are working on. To accomplish this, we are going to have to use an if...else selection structure.
- The if statement in PHP, like in C and C++, checks the condition of something and then performs a specified action based on the result of that test. We can extend an if statement by adding an else statement after it. This way, if the if statement fails, it will perform the else action.
Here is an example in plain English:
"If Dr. Giggles Magee is 3 years old, say 'yayness!', if he is anything else, say, 'I like cookies!'."
Here is the same example in PHP:
- <?php
/* if($giggleyears=="3) checks to see if the variable $giggleyears is equal to 3 */
if($giggleyears=="3") {- echo "yayness!";
- echo "I like cookies!";
?>
- We can apply the if...else selection structure to our header.php by first opening a PHP block insider our HTML title tags. The if...else statement that we want to write, is something like the following in plain English:
"If the variable $title is set, print its contents, if anything else, print our default title."
In order to test whether or not the variable $title is set, we can use a handy php function, isset(), which returns true if the variable passed to it exists, and false if it does not. So, in PHP, our if...else statement will look like this:
-
<title><?php
- if(isset($title)) {
- echo ($title);
- echo("Dr. Giggle Magee's Website");
- if(isset($title)) {
- Save the file as header.php and upload it to your Pegasus account.
- Now, when you go to includeexample.php, you will see the default title in the title bar of the website. If you go to includeexample1.php, you will see the title we specified in our title variable, which was "All About Dr. Giggles Magee"