Morelli and Smith

[article] [edit page] [discussion] [history]

From Humanitarian-FOSS Project Development Site

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http:/www.w3.org/TR/html401/loose.dtd">
<?php
/* $Id: twelvedays.php  */
/**
 *
 * Twelve Days -- A simple PHP program to "sing" the Twelve Days of Christmas song.
 *
 * This file gives a solution to a homework exercise for CPSC 225: Write a
 * PHP progam that uses an <i>associative array</i>, a <i>user-defined
 * function</i>, and a <i>foreach loop</i> to "sing" (print) the verses
 * of {@link http://www.carols.org.uk/the_twelve_days_of_christmas.htm The
 * Twelve Days of Christmas}.
 *
 * @author     R. Morelli <ralph.morelli@trincoll.edu>
 * @version    1.0
 * @package    default
 * @license    http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
 *
 */
?>

<html>
<head>
<title>Twelve Days of Christmas</title>
<?php
/**
 * An associative array of days and gifts.
 */
$days = array("first"=>"a partridge in a pear tree",
              "second"=>"two turtle doves",
              "third"=>"three French hens",
              "fourth"=>"four calling birds",
              "fifth"=>"five golden rings",
              "sixth"=>"six geese a-laying",
              "seventh"=>"seven swans a-swimming",
              "eighth"=>"eight maids a-milking",
              "ninth"=>"nine ladies dancing",
              "tenth"=>"ten lords a-leaping",
              "eleventh"=>"eleven pipers piping",
              "twelfth"=>"twelve drummers drumming");

/**
 * Prints the verses of the Twelve Days of Christmas song.
 *
 * @param array $verses Associative array of the form day1=>gift1, day2=>gift2,...
 * @access public
 * @return void
 */
function sing_twelve_days($verses)
{
    foreach ($verses as $day=>$gift)
    {
        print("On the $day of Christmas <BR>");
        print("my true love gave to me<BR>");
        print("$gift <BR><BR>");
    }
}
?>

  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#ffffff">
 <center><h4>The Twelve Days of Christmas</h4>
 <blockquote>
    <?php
      sing_twelve_days($days);    // Sing the song
      print("<h4>The end</h4>");
    ?>
 </blockquote>
</body>
</html>

Personal tools