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

Matthew Buscemi's picture
Wed, 2011-02-02 19:59
1) http://codepad.org/KPZCTxFc
  1. What are the variable types for $a, $b and $c?
  2. 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:

  1. Changing only the variables $x, $y and $z accomplish the following:
    1. make the div background red
    2. make the text ‘success’ appear on the red background div
    3. 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:

  1. The webpage will display the output of a battle between a hero and a monster.
  2. The hero begins the battle with 20 HP plus some random bonus number of hit points between 0 and 5.
  3. The monster begins the battle with 10 HP plus some random bonus number of hit points between 0 and 10.
  4. The hero does between 8 and 18 damage to the monster.
  5. The monster does between 15 and 25 damage to the player.
  6. The webpage should display the result of the battle:
    1. The webpage should display the current and max HP values of the hero and monster
    2. If the hero is still alive and the monster is dead, the page should display the message “The hero wins!”
    3. If the mosnter is still alive and the hero is dead, the page should display the message “The monster wins!”
    4. 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:

  1. 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 <=.
  2. Write a third for loop using $z.  This for loops should contain a list of all even numbers between 0 and 10 inclusive.
  3. 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:

  1. 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.
  2. 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().
  3. 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.
  4. 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.