Suppose we want to write php code that blends two words, concatenating the first half of the first word and the second half of the second word: 1
blend("motor", "hotel") "motel" blend("smoke", "fog") "smog" blend("Ian","Barland") "Iland";In the case of odd-length words, we'll "round down" the midpoint (this is handy since its what quotient (integer-division) by 2 does).
The first step in writing any program, as always, is to come up with test cases,
including degenerate inputs.
(Why do we bother with trivial/degenerate inputs?
To make any debugging easy:
If the program we're going to write goofs up on the empty string,
that will be an easy case to track down and debug.)
blend("a","") "" blend("","a") "a" blend("","abc") "bc" blend("abc","") "a" blend("","") "" blend("a","1") "1" blend("ab","12") "a2" blend("motor","hotel") "motel" blend("smoke","fog") "smog" blend("Ian","Barland") "Iland"
In php:
<?php $fname = "Ian"; $lname = "Barland"; $fHalf = substr($fname, 0, strlen($fname)/2 ); $lHalf = substr( $lname, strlen($lname)/2 ); $theBlend = $fHalf . $lHalf; echo "Hi ", $fname, " ", $lname, ".\n"; echo "I shall call you ", $theBlend; ?>We can test this by running php blend.php from the command line.
We can make functions with the keyword “
<?php function blend( $fname, $lname ) { $fHalf = substr($fname, 0, strlen($fname)/2 ); $lHalf = substr( $lname, strlen($lname)/2 ); $theBlend = $fHalf . $lHalf; return $theBlend; } echo "Hi Ian Barland. " echo "I shall call you ", blend("Ian","Barland"); ?> |
// Print an error message if the two arguments // aren't the same. // function test($actual,$expected) { if ($actual === $expected) { echo "."; } else { echo "Expected \"", $expected, "\",\n"; echo "but got \"", $actual, "\".\n"; } } test( blend("a","") , "" ); test( blend("","a") , "a" ); test( blend("","abc") , "bc" ); test( blend("abc","") , "a" ); test( blend("","") , "" ); test( blend("a","1") , "1" ); test( blend("ab","12") , "a2" ); test( blend("motor","hotel") , "motel" ); test( blend("smoke","fog") , "smog" ); test( blend("Ian","Barland") , "Iland" ); |
Final step: Let's put this program on the web!
// ... definitions of `blend` as above... echo ""; echo " |
The total program, cleaned up:
<?php // A flag to turn the test-cases on/off. define('RUN_TESTS',false); /** Return a blend of two words: * @param $word1 The word to take the first half from. * @param $word2The word to take the first half from. * @return the first half of $word1 concatenated to the second half of $word2. * (If a word is odd length, we round the halfway-point down.) */ function blend( $word1, $word2 ) { $fHalf = substr($word1, 0, strlen($word1)/2 ); $lHalf = substr( $word2, strlen($word2)/2 ); return $fHalf . $lHalf; } /** Check whether the actual and expected values are the same. * If not, print an error message. * @param $actual The actual result of the function-call. * @param $expected The expected (desired) result of the function-call. */ function test($actual,$expected) { if ($actual === $expected) { echo "."; } else { echo "Expected \"", $expected, "\",\n"; echo "but got \"", $actual, "\".\n"; } } if (RUN_TESTS) { test( blend("a", ""), "" ); test( blend("", "a"), "a" ); test( blend("", "abc"), "bc" ); test( blend("abc", ""), "a" ); test( blend("", ""), "" ); test( blend("a", "1"), "1" ); test( blend("ab", "12"), "a2" ); test( blend("motor", "hotel"), "motel" ); test( blend("smoke", "fog"), "smog" ); test( blend("Ian", "Barland"), "Iland" ); } // Produce a valid web page: echo "<html>"; echo " <head><title>Rawr</title></head>"; echo " <body>"; echo " Hi Ian Barland -- I shall call you ", blend("Ian","Barland"), "\n"; echo " (How do you like that?)"; echo " </body>"; echo "</html>"; ?> |