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.
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.
Here are some useful general debugging tips:
T_VARIABLE
and $end
. If you don't understand an error message, try Googling for the error message to see if others have seen this kind of error before. You may be able to find an explanation of what causes this problem and how to fix it.
print
statements for simple variables and print_r
or var_dump
for arrays and objects. (The XDebug software we have installed on Webster will show the values of variables when errors occur, but you may need more than this.)
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:
We suggest you code this page by following these incremental steps:
There are some stylistic aspects of the page that you should be careful about:
print
statements and should never print
any HTML tags.
if/else
statements.
When you're finished with your page, the code might look like the following sample solution, written by Sylvia Tashev and Stefanie Hatcher: