RU beehive logo ITEC dept promo banner
ITEC 325
2013spring
ibarland

homelectsexamshws
D2Lbreeze (snow day)

lect01b
php intro
functions; running via web server

Our First PHP code

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 it's 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;
?>
If we put the above lines into a file named, say, “blend.php” then we can run it (after connecting to rucs.radford.edu, using ssh or putty) by running php blend.php from the command line.

We can make functions with the keyword “function”, curly-brackets, and “return”:

<?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");
?>

Testing our functions, in php

In practice, we'd of course want to run the test-cases we'd constructed earlier:

    <?php
        /** Return a blend of two words:
         * @param $word1 The word to take the first half from.
         * @param $word2 The word to take the second 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;
        }
        
        
        // Run our tests:
        echo "\nexpect: " . "motel";
        echo "\nactual: " . blend("motor", "hotel");
        
        echo "\nexpect: " . "smog";
        echo "\nactual: " . blend("smoke", "fog");
        
        echo "\nexpect: " . "Iland";
        echo "\nactual: " . blend("Ian", "Barland");
        
        echo "\nexpect: " . "";
        echo "\nactual: " . blend("a", "");
        
        echo "\nexpect: " . "a";
        echo "\nactual: " . blend("", "a");
        
        echo "\nexpect: " . "bc";
        echo "\nactual: " . blend("", "abc");
        
        echo "\nexpect: " . "a";
        echo "\nactual: " . blend("abc", "");
        
        echo "\nexpect: " . "";
        echo "\nactual: " . blend("", "");
        
        echo "\nexpect: " . "1";
        echo "\nactual: " . blend("a", "1");
        
        echo "\nexpect: " . "a2";
        echo "\nactual: " . blend("ab", "12");
        
        
        
        echo "\n";
        echo "    Hi Ian Barland -- I shall call you ", blend("Ian","Barland"), "\n";
        echo "    (How do you like that?)\n";
        ?>

Alternately, we could also just have the computer compare the actual and expected output: Let's look at lect01b-blend-v1.txt
(We will talk about php's === vs. == later.)

… of course looking at the string of true/false results can be a bit cryptic; We can use an if-else to be a bit more user-friendly. Note that if-else looks just like it does in Java:

    <?php
    /** Return a blend of two words.
     * @param $word1 The word to take the first half from.
     * @param $word2 The word to take the second 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;
    }
    
    
    if (blend("a", "")          === "") {
      echo '(blend("a", "")          === "")', ": test passes\n";
      }
    else {
      echo '(blend("a", "")          === "")', ": test fails\n";
      }
    
    if (blend("", "a")          === "a") {
      echo '(blend("", "a")          === "a")', ": test passes\n";
      }
    else {
      echo '(blend("", "a")          === "a")', ": test fails\n";
      }
    
    if (blend("", "abc")          === "bc") {
      echo '(blend("", "abc")          === "bc")', ": test passes\n";
      }
    else {
      echo '(blend("", "abc")          === "bc")', ": test fails\n";
      }
    
    if (blend("abc", "")          === "a") {
      echo '(blend("abc", "")          === "")', ": test passes\n";
      }
    else {
      echo '(blend("abc", "")          === "")', ": test fails\n";
      }
    
    if (blend("", "")          === "") {
      echo '(blend("", "")          === "")', ": test passes\n";
      }
    else {
      echo '(blend("", "")          === "")', ": test fails\n";
      }
    
    if (blend("a", "1")          === "a") {
      echo '(blend("a", "1")          === "a")', ": test passes\n";
      }
    else {
      echo '(blend("a", "1")          === "a")', ": test fails\n";
      }
    
    if (blend("ab", "12")          === "a2") {
      echo '(blend("ab", "12")          === "a2")', ": test passes\n";
      }
    else {
      echo '(blend("ab", "12")          === "a2")', ": test fails\n";
      }
    
    if (blend("motor", "hotel")          === "motel") {
      echo '(blend("motor", "hotel")          === "motel")', ": test passes\n";
      }
    else {
      echo '(blend("motor", "hotel")          === "motel")', ": test fails\n";
      }
    
    if (blend("smoke", "fog")          === "smog") {
      echo '(blend("smoke", "fog")          === "smog")', ": test passes\n";
      }
    else {
      echo '(blend("smoke", "fog")          === "smog")', ": test fails\n";
      }
    
    if (blend("Ian", "Barland")          === "Iland") {
      echo '(blend("Ian", "Barland")          === "Iland")', ": test passes\n";
      }
    else {
      echo '(blend("Ian", "Barland")          === "Iland")', ": test fails\n";
      }
    
    
    
    
    
    echo "    Hi Ian Barland -- I shall call you ", blend("Ian","Barland"), "\n";
    echo "    (How do you like that?)";
    ?>

Sometimes, you can use the ternary operater cond ? thenStmt : elseStmt as a more concose form of if-else:

    <?php
    /** Return a blend of two words:
     * @param $word1 The word to take the first half from.
     * @param $word2 The word to take the second 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;
    }
    
    
    
    echo '(blend("a", "")          === "")', 
         ": test ", 
         (blend("a", "")          === "") ? "passes" : "fails", 
         "\n";
    echo '(blend("", "a")          === "a")', 
         ": test ", 
         (blend("", "a")          === "a") ? "passes" : "fails",
         "\n";
    echo '(blend("", "abc")         === "bc")', 
         ": test ", 
         (blend("", "abc")          === "bc") ? "passes" : "fails",
         "\n";
    echo '(blend("abc", "")          === "a")', 
         ": test ", 
         (blend("abc", "")          === "a") ? "passes" : "fails",
         "\n";
    echo '(blend("", "")          === "")', 
         ": test ", 
         (blend("", "")          === "") ? "passes" : "fails",
         "\n";
    echo '(blend("a", "1")          === "1")', 
         ": test ", 
         (blend("a", "1")          === "1") ? "passes" : "fails",
         "\n";
    echo '(blend("ab", "12")         === "a2")', 
         ": test ", 
         (blend("ab", "12")          === "a2") ? "passes" : "fails",
         "\n";
    echo '(blend("motor", "hotel")         === "motel")', 
         ": test ", 
         (blend("motor", "hotel")          === "motel") ? "passes" : "fails",
         "\n";
    echo '(blend("smoke", "fog")         === "smog")', 
         ": test ", 
         (blend("smoke", "fog")          === "smog") ? "passes" : "fails",
         "\n";
    echo '(blend("Ian", "Barland")         === "Iland")', 
         ": test ", 
         (blend("Ian", "Barland")          === "Iland") ? "passes" : "fails",
         "\n";
    
    
    
    
    
    echo "    Hi Ian Barland -- I shall call you ", blend("Ian","Barland"), "\n";
    echo "    (How do you like that?)";
    ?>

While we're busy writing functions, we can make an easy little function to actually run those test cases we created earlier:

<?php

// Code for blend, as above...



// 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" );
?>

(Anticipated ending-point, for 2013.Jan.27.)

Final step: Let's put this program on the web!


The final program, cleaned up:

    <html>
    <head>
      <title>Rawr</title>
    
    
    <?php
    
    // Turn this flag off, to suppress output for test cases that pass:
    $SHOW_PASSED_TESTS = true;
    // (Note that we will always run the test cases,
    // and we'll always print any failed tests.)
    //
    // Also, for future: consider using a php constant instead of a variable:  
    //    define('SHOW_PASSED_TESTS',true);    
    // and then later refer to SHOW_PASSED_TESTS (no preceding $)
    
    
    
    
    
    /** Return a blend of two words:
     * @param $word1 The word to take the first half from.
     * @param $word2 The word to take the second 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) {
      global $SHOW_PASSED_TESTS;
      if ($actual !== $expected) {
        echo "Test failed:\n";
        echo "Expected \"", $expected, "\",\n";
        echo "but got  \"", $actual,   "\".\n";
      }
      else {
        if ($SHOW_PASSED_TESTS) { echo "."; }
      }
    }
    
    
    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" );
    ?>
    </head>
    
    
      <body>
        Hi Ian Barland -- I shall call you <?php echo blend("Ian","Barland") ?>.
        (How do you like that?)
      </body>
    
    </html>


1 In real linguistics, a blend probably divides on syllables at least; see www.montgomerycollege.edu/~steuben/blendwords.htm. But we'll stay simple-minded and just blend by word-length.      

homelectsexamshws
D2Lbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2013.Feb.01 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme