Workshop07/Jokes solution

Back to outline

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http:/www.w3.org/TR/html401/loose.dtd">
<?php
/* $Id: jokes.php  */
/**
 *
 *  -- A simple PHP program to "tell" a few light bulb jokes.
 *
 * Problem statement: Use an <i>associative array</i>, a <i>user-defined
 * function</i>, and a <i>foreach loop</i> to print several light bulb 
 * jokes. See {@link http://www.weirdity.com/jokes/light-bulb.shtml Low Engery 
 * Light Bulb Jokes}.

 * @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>Light Bulb Jokes</title>
<?php
/**
 * An associative array of targets and punchlines.
 */
$light_bulb_jokes = array("academics"=>"None. That's what students are for.",
               "accountants" => "What answer did you have in mind?",
               "computer programmers" => "Two; one always leaves in the middle of the project.");

/**
 * Prints the jokes
 *
 * @param array $jokes Associative array of the form target1 => punchline1,...
 * @access public
 * @return void
 */
function print_jokes($jokes)
{
    foreach ($jokes as $target=>$punchline)
    {
        print("<b>Question:</b> How many  $target does it take to change a light bulb? <BR>");
        print("<b>Answer:</b> $punchline <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>Some Light Bulb Jokes</h4>
 <blockquote>
    <?php
      print_jokes($light_bulb_jokes);    // Print the jokes
      print("<h4>The end</h4>");
    ?>
 </blockquote>
</body>
</html>

Back to outline