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

homelectshws
D2Lbreeze (snow day)

hw01
hw01: A first php program

Due 2014.Jan.27 (Mon) 23:59.

For this homework, you'll submit three files:

Notes:

Note that for this assignment, we are not making any web pages! We are simply writing php functions which return strings; some those strings just happen to have angle-brackets in them, and they happen to be html elements, but we're not (yet) including that into any web page.

  1. (10pts) Write a php function string pluralize( int $num, string $noun ) which returns a user-friendly version of a number+noun: pluralize( 7, "t-shirt" ) === "7 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. But what other corner-cases should you handle? (Many students lose points by not thinking through test cases closely enough, and then not having code that covers such corner cases.)
    2. You can presume that a plural is always formed by just adding “s” to any word.
      (A small amount of extra credit: incorporate more-correct rules about how plurals are formed in English.)

  2. The following two problems are not due yet — they will be on the next homework. However, you can start on them earlier if you like. Note that if-statements use the same syntax as Java.

  3. NOT DUE YET — will be on the next hw. (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!
    The fact that the strings happen to have angle-brackets in them means that the result might be useful to people generating html, but the function itself does't really care.

    (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 don't contain any quote-characters, but the returned result does (since HTML attributes must be quoted). I recommend that your generated html uses single quote marks around attributes, but you'll get full credit for either single- or double-quote marks, as long as it is valid HTML. Make sure your test case(s) express exactly what you want the desired output should be!

  4. NOT DUE YET — will be on next hw. (10pts) Thumbnails:
    1. What is the html element for an image with source pony.jpg (in the same directory), that is rendered 300px wide?
      Include your answer in a comment, in your test-file.
    2. What is the html for such an image which is also a link — where the link would take you to that file (not a html page — just that file).
      Include your answer in a comment, in your test-file.
    3. Make a test-case for thumbnail("pony.jpg",300); the expected-output should be a string that happened to be your answer.
      You don't need further test-cases for this problem.

      Pay close attention to the exact spacing you want to have. I recommend using single-quotes (') around the attribute-values. (We'll discuss this more on Monday.)

    4. Finally, implement the function thumbnail that takes in a string (a URL) and a number (the width in pixels), and returns a string that is an html link containing an image.

      For full credit, your function should call hyperlink as already written!


1 You may have seen in Web I that text not enclosed in <?php … ?> is a short-cut for printing; we'll use that on following assignments, but for this this assignment use explicit echo (or, print) statements.      

homelectshws
D2Lbreeze (snow day)


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