RU beehive logo ITEC dept promo banner
ITEC 325
2012fall
ibarland

homelectsexamshws
D2Lbreeze (snow day)

hw01
hw01: A first php program

Due Sep.11 (Tue) 23:59.
I'll grade by visiting https://php.radford.edu/~yourUserId/itec325/hw01/hw01.php. If I can't visit that file, you won't get any credit! Do not change the file after the due-date; instead make a copy of the directory1 if you want to make changes later.
  1. (10pts) Write a php function string pluralize( int $num, string $noun ) which returns a user-friendly version of a number+noun: pluralize( 3, "t-shirt" ) === "3 t-shirts". We'll use this function on future web-pages, when (say) confirming somebody's order.
    1. Be sure to include at least three test cases, each covering a different situation.
      (The noun will never be the empty string, so you don't even need to check for that.)
      You may use the same bare-bones test function as used in lect01b-blend-v7.php (but cite the author or source, if you do). Remember, a test case includes both a input and an expected output. Running the test cases means having the computer check that the actual-output is exactly the expected output.
    2. You can presume that a plural is always formed by just adding “s” to any word.
      (Extra credit, open-ended: incorporate more-correct rules about how plurals are formed in English.)
    You should run your php program locally (from the command-line) to make sure it works and passes all your test cases.
  2. (10pts) Write a function string hyperlink( string $url, string-or-boolean $linkTxt ) which returns a string of html. If the provided $linkTxt is false (instead of a string), then use the $url as the link text.
    hyperlink( 'http://www.gutenberg.org', 'free books!' ) === "<a href='http://www.gutenberg.org'>free books!</a>"
    hyperlink( 'myLocalFile.html', false ) === "<a href='myLocalFile.html'>myLocalFile.html</a>"
    Note that this is just a regular ol' function that takes in strings and returns strings!
    (You don't need to come up with additional test cases, but you do need to actually include these tests or their equivalent. You can assume that neither string is ever empty.) Note how the input strings probably don't contain any quote-characters, but the returned result does (since HTML attributes must be quoted). Your HTML result can use either single- or double-quote marks, as long as it is valid HTML.
  3. (4pts) Modify your program so that it prints out a (bare-bones) web page, including the results calling each of your functions at least once (e.g. “99 bottles of rootbeer on the wall”).
  4. (1pt) Finally, to help me grade, finish your web page with a h3 header mentioning your program's name (~yourUserId/hw01.php) and then calling show_source( 'hw01.php' );. Note that this function prints a large amount of html (and if/when you run your program on the command-line with php hw01.php, you'll see screenfuls of html scroll by).
  5. Finally, modify your program so that it only shows the source if the date is more than 15 minutes past this homework's deadline. Before then, your program will print the above output followed by a message to the effect “The php source will only be displayed after date”. Also include the file's last-modified date:
    echo "Last modified: ", date("Y.M.d (D) H:i.", filemtime('hw01.php'));
    Here is an example file which uses these date shenanigans.

1 The unix command cp -pR existingDir/ newCopy will Recursively copy a file, preserving the file-times.      

homelectsexamshws
D2Lbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2012.Sep.11 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme