Student E, Student F, and Student G

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

From Humanitarian-FOSS Project Development Site

12 Days of PHP

<html>
<title>The 12 Days of Christmas</title>
<head>
</head>

<body>

<?php
/**
* This file contains the code to print out the 12 Days of Christmas song, utilizing an associative array.
*
 * @author Student F
 * @version 1.0
 * @package default
*/



$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",
   	"eigth"=>"Eight Maids A-milking",
   	"ninth"=>"Nine Ladies Dancing",
   	"tenth"=>"Ten Lords A-leaping",
   	"eleventh"=>"Eleven Pipers Piping",
   	"twelfth"=>"Twelve Drummers Drumming");	

print "<center>";	
sing($days);
print "</center>";



/**
 * Function that will print out the lyrics of Twelve Days of Christmas.
 * It will sing the song in the format given on http://www.carols.org.uk/the_twelve_days_of_christmas.htm
 *
 * @param array $lyric
 */
function sing($lyric)
{
	
   	$i=0;	
	foreach ($lyric as $number=>$gift){
		print "On the {$number} day of Christmas,<br>";
		print "my true love sent to me <br>";
		print $gift;
	    sing_partly(array_reverse(array_slice($lyric,0,$i)));
		print "<br><br>";
		$i++;
		
	}
}

/**
 * Helper function of sing(). It will print out the song in an array with a comma in
 * between and the last element will be "AND ... ."
 *
 * @param array $array
 */

function sing_partly($array){
	
	$size = sizeof($array);
	$i=0;
	
	if($size != 0){
		foreach($array as $number=>$gift){
			print ",<br>";
			if($i != ($size-1)){
				print $gift;
			}else{
				print "And ".$gift.".";
			}
			$i++;
		}
	}
}
?>
</body>
</html>

This code is implemented in module in WeSahana.

Personal tools