JavaScript Editor Javascript validator     Website design 



Team LiB
Previous Section Next Section

Improving "This Old Man" with Arrays and Loops

The basicArray.php program shows how to build arrays, but it doesn't illustrate the power of arrays and loops working together. To see how these features can help you, let's revisit an old friend from the last Chapter 3, "Controlling Your Code with Conditions and Functions." The version of the "This Old Man" program featured in Figure 4.8 looks a lot like it did in Chapter 3, but the code is quite a bit more compact.

Click To expand
Figure 4.8: The Fancy Old Man program uses a more compact structure that is easy to modify.

The improvements in this version of the program are only apparent when you look under the hood.

<html>
<head>
<title>
Fancy Old Man
</title>
</head>
<body>
<h1>This Old Man with Arrays</h1>
<pre>
<?
$place = array(
  "",
  "on my thumb",
  "on my shoe",
  "on my knee",
  "on a door");

//print out song
for ($verse = 1; $verse <= 4; $verse++){

print <<<HERE
 This old man, He played $verse
 He played knick-knack $place[$verse]
 ...with a knick, knack, paddy-whack
 give a dog a bone
 This old man came rolling home


HERE;
  } // end for loop

?>
</pre>
</body>
</html>

This improved version takes advantage of the fact that the only things that really change from verse to verse is the verse number, and the place where the old man plays paddy-whack (whatever that means). You can organize the places into an array, and that would greatly simplify writing out the song lyrics.

Building the Place Array

I noticed that each place is a string value associated with some number. I used the array() directive to pre-load the $place array with appropriate values. There isn't a place corresponding to zero, so I simply left the zero element blank.

$place = array(
  "",
  "on my thumb",
  "on my shoe",
  "on my knee",
  "on a door");

Note that like most places in PHP, carriage returns don't really matter when you're writing the source code. I decided to put each place on a separate line, just because it looked neater that way.

Writing Out the Lyrics

The song itself is incredibly repetitive. Each verse is identical with the others except for the verse number and the corresponding place. For each verse, the value of the $verse variable will be the current verse number. The corresponding place is stored in $place[$verse]. The code to print out the entire song is a large print statement in a for loop.

//print out song
for ($verse = 1; $verse <= 4; $verse++){

print <<<HERE
 This old man, He played $verse
 He played knick-knack $place[$verse]
 ...with a knick, knack, paddy-whack
 give a dog a bone
 This old man came rolling home


HERE;
  } // end for loop

The Fancy Old Man program illustrates very nicely the trade-off associated with using arrays. Creating a program that uses arrays correctly often takes a little more planning than using control structures alone (as the programs in Chapter 3). However, the extra work up front can really pay off because the program can be much easier to modify and extend.


Team LiB
Previous Section Next Section


JavaScript Editor Javascript validator     Website design


R7