University of Washington, CSE 190 M
Section 3: Twelve Days of Xmas
Basic PHP

section idea and code by former TAs Sylvia Tashev and Stefanie Hatcher

This section is about writing basic pages using PHP, as well as finding and fixing bugs in PHP code.

1. PHP syntax:

In lecture we started to learn the basics of PHP syntax, but we did not get very far. So let's go through basic PHP syntax for things like variables, loops, and if/else. Once you feel comfortable with the basics, move on.

2. Buggy PHP programs:

The following PHP pages each contain one or more bugs. The bugs could be syntax errors or incorrect logic. Look at each page, find the bug(s), and correct the errors.

  1. buggy page 1 (source, as HTML) (source, as text)
  2. buggy page 2 (source, as HTML) (source, as text)
  3. buggy page 3 (source)
  4. buggy page 4 (source)

Here are some useful general debugging tips:

12 Days of Xmas:

Write a page that displays the complete lyrics of the song, "12 Days of Xmas," along with displaying images for each of the gifts given on each day. Start from the template in the following file:

The page already has the correct appearance, but the code is long and redundant. Modify the page to use PHP code to remove this redundancy. The page should look like the following:

expected output

We suggest you code this page by following these incremental steps:

  1. For each day # N of Xmas, show day N's gift image once.
  2. For each day # N of Xmas, show day N's gift image N times.
  3. For each day # N of Xmas, show (day N's down to day 1's) gift image the appropriate number of times.
  4. (advanced) Add the text labels such as "Three French hens" under each gift's image(s). You probably won't be able to finish that in the time provided. You may want to use advanced syntax such as arrays to help you with this.

When you're finished with your page, the code might look like the following sample solution, written by former TAs Sylvia Tashev and Stefanie Hatcher:

Valid XHTML 1.1 Valid CSS!