| CS 199 | Willamette University | Spring, 2019 |
|
Programming in PHP, Databases with MySQL, and Web Applications |
||
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.
This lab considers tailoring Web-page content, based on such local conditions and the time and the day.
The next lab explores the possibility of responding to user-supplied data within a Web page.
Read Butler and Yank, pages 15-23, before starting this laboratory exercise!
Consider Web page: date-time-stuff.php
Click on data-time--stuff.php, and then reload the page several times.
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.
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.)
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).
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.
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
The textbook describes the rand which returns a random integer.
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 |
|