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

homelectshws
D2Lbreeze (snow day)

lect02-separate-test-files
separate php files
code, testing, html-generating

Last time, we talked about writing test cases and code for a function blend. Today we'll show more ways of running php, and how to properly separate files.

Accessing rucs

Before we even revisit the code, let's talk about rucs:

Separating files

We left our program as follows:

 1  <?php
 2      /** Return a blend of two words:
 3       * @param $word1 The word to take the first half from.
 4       * @param $word2 The word to take the second half from.
 5       * @return the first half of $word1 concatenated to the second half of $word2.
 6       *   (If a word is odd length, we round the halfway-point down.)
 7       */
 8  function blend( $str1, $str2 ) {
 9    $mid1 = strlen($str1)/2;
10    $mid2 = strlen($str2)/2;
11    $firstHalf = substr($str1, 0, $mid1 );
12    $secondHalf = substr($str2, $mid2 );
13    return $firstHalf . $secondHalf;
14    }
15  
16  
17  
18  
19  
20  
21  /*
22      function blend( $word1, $word2 ) {
23        $fHalf = substr($word1, 0, strlen($word1)/2 );
24        $lHalf = substr( $word2, strlen($word2)/2 );
25        return $fHalf . $lHalf;
26      }
27      
28  */
29      
30      // Run our tests:
31      echo "\nexpect: " . "motel";
32      echo "\nactual: " . blend("motor", "hotel");
33      
34      echo "\nexpect: " . "smog";
35      echo "\nactual: " . blend("smoke", "fog");
36      
37      echo "\nexpect: " . "Iland";
38      echo "\nactual: " . blend("Ian", "Barland");
39      
40      echo "\nexpect: " . "";
41      echo "\nactual: " . blend("a", "");
42      
43      echo "\nexpect: " . "a";
44      echo "\nactual: " . blend("", "a");
45      
46      echo "\nexpect: " . "bc";
47      echo "\nactual: " . blend("", "abc");
48      
49      echo "\nexpect: " . "a";
50      echo "\nactual: " . blend("abc", "");
51      
52      echo "\nexpect: " . "";
53      echo "\nactual: " . blend("", "");
54      
55      echo "\nexpect: " . "1";
56      echo "\nactual: " . blend("a", "1");
57      
58      echo "\nexpect: " . "a2";
59      echo "\nactual: " . blend("ab", "12");
60      
61      
62      
63      echo "\n";
64      echo "    Hi Ian Barland -- I shall call you ", blend("Ian","Barland"), "\n";
65      echo "    (How do you like that?)\n";
66      ?>

We can use require-once to separate these better: one file for library functions (“blend.php”); one for test cases (“blend-test.php”); an end "app" file that actually does/print what we want (“lect02-page-printer.php”):

homelectshws
D2Lbreeze (snow day)


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