This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
Introduction to PHP
Assignments for Week #3 - Taking Control of your Code
1) http://codepad.org/KPZCTxFc
-
What are the variable types for $a, $b and $c?
-
In what situations would it be better to use the equality operator as opposed to the identity operator and vice versa?
2) http://codepad.org/Z2IBem4R - Modify this webpage code chunk according to the following parameters:
-
Changing only the variables $x, $y and $z accomplish the following:
-
make the div background red
-
make the text ‘success’ appear on the red background div
-
make the text ‘success’ appear on the blue background div
3) http://codepad.org/hWz09tmF - Using this code chunk as an example, build the following page of your own:
-
The webpage will display the output of a battle between a hero and a monster.
-
The hero begins the battle with 20 HP plus some random bonus number of hit points between 0 and 5.
-
The monster begins the battle with 10 HP plus some random bonus number of hit points between 0 and 10.
-
The hero does between 8 and 18 damage to the monster.
-
The monster does between 15 and 25 damage to the player.
-
The webpage should display the result of the battle:
-
The webpage should display the current and max HP values of the hero and monster
-
If the hero is still alive and the monster is dead, the page should display the message “The hero wins!”
-
If the mosnter is still alive and the hero is dead, the page should display the message “The monster wins!”
-
If both hero and monster are still alive, the page should display ‘Draw!’
4) http://codepad.org/hxbxnCmz - Modify this code chunk according to the following parameters:
-
Make both the $x for loop and the $y for loop display the same list of numbers from 2 to 8. The second expression in the $x for loop must use < and the second expression in the $y for loop must use <=.
-
Write a third for loop using $z. This for loops should contain a list of all even numbers between 0 and 10 inclusive.
-
Write a new for loop with $w. This loop should start with 1, and each number should be double the last number: 1, 2, 4, 8, etc. Have the loop stop after it prints 1024.
5) Modify the webpage you wrote for part 3 in the following way:
-
Have the monster and hero deal damage to each other three times instead of just once. The PHP code should recalculate the damage each turn. It should not do one damage calculation, and repeat that damage three times in a row.
-
Reduce the amount of damage done by the hero and monster in each round by one third. Do not have either the hero or monster do damage with floating point numbers. You will want to use at least one of the following functions to ensure this: round(), floor(), ceil().
-
Each turn, print the turn number, the amount of damage done by the monster and hero to one another, and the current and max hit points of each. Do not let the hero or the monster deal damage if either is dead.
-
Display the result text after the battle is completed.
6) Create an exercise for your classmates that requires the use of if, else, elseif or for statements (or any combination of the above). The exercise can have your classmate analyze your code chunk on Codepad or provide instructions for them to build their own, or both.