0290280 - MacKay Wilford

Weekly assignment for lesson 1

Exercise no. 1

Required

Description

Please create a script file called phpinfo.php somewhere on your web site. The file should contain all the necessary instructions for printing the PHP's environment information. (You can find an example of this in the lecture.)

Once you have done this and tested it, please replace the word "URL" in source of the the "My PHP Info file" link below with the actual URL, then report the values for the three settings listed below.

To do this, simply load this file (index.php) into your text editor and edit the appropriate lines (see orange highlights):

Text editor
  1. register_globals: On
  2. magic_quotes_gpc: On
  3. display_errors: On

For security reasons, don't publish this link outside of this class!

Quick Link

Exercise no. 2

Required

Description

Create file called form.html in the lesson1 directory. This file should contain a form with the following contraints and fields:

You are free to decide the form's METHOD attribute, field names and the layout.

Goal: Prepare a form in a static HTML page. When the user fills the form and submits the data, the browser sends the data to the URL specified in the ACTION attribute (in this case exercise3.php); its tasks are explained in the following exercise.

Quick Link

Grading (for instructors)

Exercise no. 3

Required

Description

Use the file called exercise3.php in the lesson1 directory to create a script which processes the input from the form you created in exercise 2. Your script should:

  1. practice using one of the two print commands used for debugging: Use either print_r() or var_dump() to print the raw input of the $_POST or $_GET array (depending on which METHOD you chose in Ex. 2). (Note that in the real world, you would not use these in a live script, only while you are testing);
  2. test the numeric field to see if it is a number using the if/else construct;
  3. print whether the numeric field is a number or not. Be safe: If the data fails your test, be sure to print only a generic error message, not the data itself!

IMPORTANT: If your register_globals setting is turned on, be sure that you do not use the registered global variables without over-writing them. Example: For a field named "age," the correct variable name is $_POST['age'], not $age.

Quick Link

Grading (for instructors)

Exercise no. 4

Optional

Description

Although this exercise is optional, we highly recommend that you use it to practice some of the concepts we introduced this week.

In line with this week's objectives, you will be using numbers, some of the most common mathematical operators and two different if constructs in order to choose different paths for computation and output.

This is a very basic die game. We will simulate two rolls of a die, display the values, then determine the minimum and the maximum of the two using an if/elseif/else construct. (You need to handle the less likely case of identical values, too.) We'll also use this information to play with some of the mathematical operators: Depending on the min/max/same outcome, we will display various calculations.

How do we "roll" a die? That is, how do we randomly generate a number from 1 to 6? PHP makes life easy there: It has a built-in function called rand(). Don't worry! You don't need to know it; we have provided that code.

Here is the detailed list of tasks your appplication needs to do:

  1. Roll the die once and display:
    1. the roll value (e.g.: 1)
    2. whether the roll value is even or odd (e.g.: odd)
  2. Roll the die a second time and display:
    1. the roll value (e.g.: 4)
    2. whether the roll value is even or odd (e.g.: even)
  3. The remaining output is based on the relationship of the first and second rolls:
    1. If roll 1 > than roll 2, print:
      • 'the first roll is greater than the second'
      • the difference between the first and the second roll
    2. If roll 1 < than roll 2, print:
      • 'the second roll is greater than the first'
      • the sum of the two rolls
      • the average of the two rolls
    3. If roll 1 = roll 2, print:
      • 'the rolls are equal'
      • the product of the two

Modify the file called exercise4.php in the lesson1 directory as base for your exercise.

Notes and Hints

Here's how we generate a random number within a range using the rand() function. (Please spend a minute looking at the reference page for this function.)

// Let's roll the die for the first time $first_roll = rand(1,6); // random generation

How do you determine whether a number is odd or even? A common shortcut is to use modulus 2. If you are unfamiliar with modulus, or have forgotten it from disuse, the modulus operator calculates the remainder of a division operation. Modulus 2 means that we use 2 as the divisor. If a number is even, division by two leaves a remainder of 0; if it is odd, there is a remainder of 1. This gives us a quick and easy shortcut for determing even or odd:

$first_remainder = $first_roll % 2;

At this point, you should be able to go on by yourself. If you have any questions, please don't hesitate to write to the forum and ask us. Have fun!

Quick Link

Grading (for instructors)