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.
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.
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.
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 page should look like the following:
We suggest you code this page by following these incremental steps:
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: