LIGHTENING YOUR WORKLOAD WITH INCLUDES
81
filenames, get out of them immediately. Spaces are not allowed in URLs, which is why most
web design software replaces spaces with %20. You can get around this problem, though, by
using an underscore.
Change the filename of contact.php to contact_us.php.
8. Amend the code in title.inc.php like this:
<?php
$title = basename($_SERVER['SCRIPT_FILENAME'], '.php');
$title = str_replace('_', ' ', $title);
$title = ucwords($title);
The middle line uses a function called str_replace() to look for every underscore and
replace it with a space. The function takes three arguments: the character(s) you want to
replace, the replacement character(s), and the string you to change.
You can also use
str_replace()
to remove character(s) by using an empty string (a pair of quotes
with nothing between them) as the second argument. This replaces the string in the first argument
with nothing, effectively removing it.
Instead of ucfirst(), the final line of code uses the related function ucwords(), which gives
each word an initial cap.
9. Save title.inc.php, and load the renamed contact_us.php into a browser. Figure 4-7
shows the result.
Figure 4-7. The underscore has been removed, and both words have been given initial caps.
10. Change the name of the file back to contact.php, and reload the file into a browser. The script
in title.inc.php still works. There are no underscores to replace, so str_replace() leaves
the value of $title untouched, and ucwords() converts the first letter to uppercase, even
though theres only one word.
11. The home page of the Japan Journey site is called index.php. As Figure 4-8 shows, applying
the current solution to this page doesnt seem quite right.
CHAPTER 4
82
Figure 4-8. Generating the page title from index.php produces an unsatisfactory result.
There are two solutions: either dont apply this technique to such pages or use a conditional
statement (an if statement) to handle special cases. For instance, to display Home instead
of Index, amend the code in title.inc.php like this:
<?php
$title = basename($_SERVER['SCRIPT_FILENAME'], '.php');
$title = str_replace('_', ' ', $title);
if ($title == 'index') {
$title = 'home';
}
$title = ucwords($title);
The first line of the conditional statement uses two equal signs to check the value of $title.
The following line uses a single equal sign to assign the new value to $title. If the page is
called anything other than index.php, the line inside the curly braces is ignored, and $title
keeps its original value.
PHP is case-sensitive, so this solution works only if index is all lowercase. To do a case-insensitive
comparison, change the fourth line of the preceding code like this:
if (strtolower($title) == 'index') {
The function
strtolower()
converts a string to lowercasehence its nameand is frequently used
to make case-insensitive comparisons. The conversion to lowercase is not permanent, because
strtolower($title)
isnt assigned to a variable; its only used to make the comparison. To make a
change permanent, you need to assign the result back to a variable as in the final line, when
ucwords($title)
is assigned back to
$title
.
To convert a string to uppercase, use
strtoupper()
.
12. Save title.inc.php, and reload index.php into a browser. The page title now looks more
natural, as shown in Figure 4-9.
Download from Wow! eBook <www.wowebook.com>
LIGHTENING YOUR WORKLOAD WITH INCLUDES
83
Figure 4-9. The conditional statement changes the title on index.php to Home.
13. Navigate back to contact.php, and youll see that the page title is still derived correctly from
the page name.
14. Theres one final refinement you should make. The PHP code inside the <title> tag relies on
the existence of the variable $title, which wont be set if theres a problem with the include
file. Before attempting to display the contents of a variable that comes from an external
source, its always a good idea to check that it exists, using a function called isset(). Wrap
the echo command inside a conditional statement, and test for the variables existence like
this:
<title>Japan Journey<?php if (isset($title)) {echo "&#8212;{$title}";}
?></title>
If $title doesnt exist, the rest of the code is ignored, leaving the default site title, Japan
Journey.
You can check your code against title.inc.php and an updated version of index.php in
index_03.php in the ch04 folder.
Creating pages with changing content
So far, youve used PHP to generate different output depending on the pages filename. The next two
solutions generate content that changes independently of the filename: a copyright notice that updates
the year automatically on January 1 and a random image generator.
PHP Solution 4-5: Automatically updating a copyright notice
At the moment, the copyright notice in footer.inc.php contains only static HTML. This PHP solution
shows how to use the date() function to generate the current year automatically. The code also specifies
the first year of copyright and uses a conditional statement to determine whether the current year is
different. If it is, both years are displayed.
Continue working with the files from PHP Solution 4-4. Alternatively, use index_03.php and
footer.inc_01.php from the ch04 folder, and remove the numbers from the filenames. If using the files
from the ch04 folder, make sure you have copies of title.inc.php and menu.inc.php in the includes
folder.
CHAPTER 4
84
1. Open footer.inc.php. It contains the following HTML:
<div id="footer">
<p>&copy; 2006&#8211;2010 David Powers</p>
</div>
The &#8211; between the dates is the numeric entity for an en dash.
2. The advantage of using an include file is that you can update the copyright notice throughout
the site by changing this one file. However, it would be much more efficient to increment the
year automatically, doing away with the need for updates altogether.
The PHP date() function takes care of that very neatly. Change the code in the paragraph like
this:
<p>&copy; 2006&#8211;<?php echo date('Y'); ?> David Powers</p>
This replaces the second date and displays the current year using four digits. Make sure you
pass an uppercase Y as the argument to date().
3. Save footer.inc.php and load index.php into a browser. The copyright notice at the foot of
the page should look the same as before—unless, of course, youre reading this in 2011 or
later, in which case the current year will be displayed.
Like most copyright notices, this covers a range of years, indicating when a site was first
launched. Since the first date is in the past, it can be hard-coded. But what if youre creating a
new website? You dont want to have to break away from the New Year revelries just to update
the copyright notice. There needs to be a better way. Thanks to PHP, you can party to your
hearts content on New Years Eve.
4. To display a range of years, you need to know the start year and the current year. If both years
are the same, display only the current year; if theyre different, display both with an en dash
between them. Its a simple if. . .else situation. Change the code in the paragraph in
footer.inc.php like this:
<p>&copy;
<?php
$startYear = 2006;
$thisYear = date('Y');
if ($startYear == $thisYear) {
echo $startYear;
} else {
echo "{$startYear}&#8211;{$thisYear}";
}
?>
David Powers</p>
As in PHP Solution 4-4, Ive used curly braces around the variables in the else clause
because theyre in a double-quoted string that contains no whitespace.
5. Save footer.inc.php, and reload index.php in a browser. The copyright notice should look
the same as before.
LIGHTENING YOUR WORKLOAD WITH INCLUDES
85
6. Change the argument passed to the date() function to a lowercase y like this:
$thisYear = date('y');
7. Save footer.inc.php, and click the Reload button in your browser. The second year is
displayed using only the last two digits, as shown in the following screenshot:
This should serve as a reminder that PHP is case-sensitive. Uppercase Y and lowercase y produce
different results with the
date()
function. Forgetting about case sensitivity is one of the most
common causes of errors in PHP.
8. Change the argument passed to date() back to an uppercase Y. Set the value of $startYear
to the current year, and reload the page. This time, you should see only the current year
displayed.
You now have a fully automated copyright notice. The finished code is in footer.inc_02.php
in the ch04 folder.
PHP Solution 4-6: Displaying a random image
Displaying a random image is very easy. All you need is a list of available images, which you store in an
indexed array (see “Creating arrays” in Chapter 3). Since indexed arrays are numbered from 0, you can
select one of the images by generating a random number between 0 and one less than the length of the
array. All accomplished in a few lines of code . . .
Continue using the same files. Alternatively, use index_03.php from the ch04 folder and rename it
index.php. Since index_03.php uses title.inc.php, menu.inc.php, and footer.inc.php, make
sure all three files are in your includes folder. The images are already in the images folder.
1. Create a blank PHP page in the includes folder, and name it random_image.php. Insert the
following code (its also in random_image_01.php in the ch04 folder):
<?php
$images = array('kinkakuji', 'maiko', 'maiko_phone', 'monk', 'fountains',
'ryoanji', 'menu', 'basin');
$i = rand(0, count($images)-1);
$selectedImage = "images/{$images[$i]}.jpg";
This is the complete script: an array of image names minus the .jpg filename extension
(theres no need to repeat shared informationtheyre all JPEG), a random number generator,
and a string that builds the correct pathname for the selected file.
To generate a random number within a range, you pass the minimum and maximum numbers as
arguments to the function rand(). Since there are eight images in the array, you need a