Web Programming Step by Step
Discussion Section 3: Twelve Days of Xmas
Basic PHP

section idea and code by Sylvia Tashev and Stefanie Hatcher

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

Buggy PHP programs:

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

  1. buggy page 1 (source)
  2. buggy page 2 (source)
  3. buggy page 3 (source)
  4. buggy page 4 (source)
  5. buggy page 5 (source)
  6. buggy page 6 (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 last of the 12 days 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, with no text under each image.
  2. For each day # N of Xmas, (show day N's down to day 1's) gift image once each.
  3. For each day # N of Xmas, show (day N's down to day 1's) gift image N times each.
  4. Add the text labels such as "Three French hens" under each gift's image(s).

There are some stylistic aspects of the page that you should be careful about:

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

Valid XHTML 1.1 Valid CSS!