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):
register_globals
: Onmagic_quotes_gpc
: Ondisplay_errors
: OnFor security reasons, don't publish this link outside of this class!
Create file called form.html
in the lesson1
directory. This file should contain a form with the following contraints and fields:
FORM
tag's ACTION
attribute must be set to "exercise3.php
"). This text field should expect a string (e.g.: name, last name, town, colour, etc.);INPUT
HTML element with the TYPE
attribute set to "text"). This text field should expect a string (e.g.: name, last name, town, colour, etc.);TEXTAREA
tag).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.
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:
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);if/else
construct;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
.
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:
Modify the file called exercise4.php
in the lesson1
directory as base for your exercise.
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!