CS 199 Willamette University Spring, 2019
 
Programming in PHP, Databases with MySQL,
and Web Applications
 

Laboratory Exercise on PHP in HTML

Summary: HTML provides a mechanism to specify content within an Web page and to identify structural elements for that content (e.g., headings, paragraphs). Style sheets provide a way to format that content. However, writing HTML directly yields pages that are static—the content of each page remains the same every time the page is referenced.

This lab and the next introduce the possibility that a Web page can respond to changing circumstances.

Background

Read Butler and Yank, pages 15-23, before starting this laboratory exercise!

Inserting PHP within an HTML Page

Consider Web page: date-time-stuff.php

  1. Click on data-time--stuff.php, and then reload the page several times.

    1. What (if anything) seems to stay the same each time you reload, and what (if anything) changes?
    2. The bottom of the page contains an addition problem involving integers. When the page is reloaded several time, do you observe any pattern regarding the numbers being added? (Are the numbers ever negative? Are they all small (or all large)? Are the numbers always integers? Etc.) Explain briefly.
  2. Click on the text version of data-time--stuff.php, save the file to your Desktop (changing the .txt extension to .php, and open the file with TextEdit.

    1. What parts of the page seem to follow the standard format for HTML pages (e.g., with tags <h1>, <p>, <table>, etc.).
    2. Identify the parts of the file that begin with <?php and end with ?>. These Sections contain statements, written in the programming language PHP. When the Web server accesses this stored file, the Web server runs the PHP code, determines any generated output, and includes that output in the page sent to your browser. (The PHP code itself is not transmitted to your browser—just the output generated by that code.
  3. Remove the first set of markers, <?php and ?>, copy the file to the Web server, and load the revised file. Explain what is displayed. (Once done, restore these markers to the file.)

  4. Within the first PHP section, insert the following lines:

       echo "this is the first added line<br>";
       echo "this 'is the' second added line<br>";
       echo 'this is a third added line<br>";
       echo 'here is a "fourth added" line'; 
    

    Observe what happens if double quotes or single quotes are used. What happens if you mix double and single quotes (e.g., echo "here'we"go'again";)

    Overall, the echo statement indicates that something is to be printed, and the text can be put in either single or double quotes (as long as the starting and ending quotes match).

  5. The date function returns many values related to dates and times. Some common letter parameters follow:

    Duration Unit Letter Meaning
    Year Y year in four digits
    y year in two digits
    Month F Month as a full word
    M Month (3-letter abbreviation
    m Month number (01 to 12)
    n Month number (1 to 12; no leading 0's)
    Day l (lowercase L) Day as full word
    D Day (3-letter abbreviation)
    d Day number (01 to 31)
    j Day number (1 to 31 (no leading 0's)
    Hour h hour in 12-hour format (1 to 12, no leading 0's)
    H hour in 24-hour format (01 to 24)
    a am or pm
    A AM or PM
    Minutes i Minutes (00 to 59 with leading zeros)
    Seconds s Seconds (00 to 59 with leading zeros)

    A full listing of date is available from w3schools.com.

    1. In the date-time-stuff.php file, try experimenting using different parameters in the date function.
      Note that date allows a few characters ("/", ".", "-") to be used to separate the formatting characters to add some textual context. For any formatting beyond these few options, retrieve each date/time element separately, and print the resulting values within a longer string.
    2. What happens if you call the date function within single quotes (e.g., 'date("Y")')?
  6. In PHP, values (e.g., numbers, strings of characters) may be stored in variables. A variable is a sequence of letters or numbers, starting with a letter, and preceded by a dollar sign. Some examples:

      $value
      $number1
      $b1IsAVitamin
    
    1. Review the date-time-stuff.php program, and identify any variables used.
    2. After examining date-time-stuff.php, what happens when a variable is included within an echo statement?
  7. The textbook describes the rand which returns a random integer.

    1. Explain how rand is used in date-time-stuff.php.
    2. What happens if rand(1, 20) is replaced by rand{1, 50) or by rand(1, 3)?
  8. Extend date-time-stuff.php to compute and print the sum of the hours, minutes, and seconds for the current time.




created 23 January 2019
revised 23-24 January 2019
Valid HTML 4.01! Valid CSS!