In this lab, you'll write some PHP. It won't be anything very difficult :) And in fact, you don't even have to submit anything - but I hope you'll still do the work.
The real purpose of the lab is for you to get used to writing PHP scripts and running them on our computers.
Up to now, you've been doing your work in a folder called cs1102
,
which you created during Induction Week. On the (rare) occasions when I
wanted you to publish a web site, you copied your files from your
cs1102
folder to your public_html
folder, so that
the Computer Science student web server would be able to find them.
The web server needs to be able to find your PHP scripts as well. So,
they'll all have to be placed in your public_html
folder.
For this reason, it is probably easiest if you store your work there in the
first place. So that's what I suggest you do...Ready?
Inside your public_html
folder, create a new folder called
cs1102
(all lowercase). Inside your new cs1102
folder, create another folder (a subdirectory), called lab10
(all lowercase).
The following content is provided:
Download, save and unzip this this zip file. It contains just one file: template.php
.
For now, the handiest place to save it is in your
public_html\cs1102\lab10
folder.
Make a copy of template.php
. Call it fleas.php
.
Use TextPad (or your favourite text editor) to edit fleas.php
.
Between the body
tags in fleas.php
, put a paragraph.
Inside the paragraph, put a snippet of PHP. Your PHP is going to output the
world's shortest poem (which is called Fleas, by the way). This is it:
Adam
Had 'em
Use one echo
instruction per line of poem. (And think about the
apostrophe!)
Save the file.
Now we want to run it.
What you've been doing previously, when we were writing static web pages such as
uzbekistan.html
, was simply to
double-click the file icon. As you know, this brought up your browser and it would
be displaying the appropriate web page.
This won't work for PHP scripts. (Try it if you want.) The reason is: this loads the web page directly into the browser.
But what we need (as explained in the lecture) is to send a request to the server, and the server will then get the PHP processor to run the script. (Remember all that stuff about copy mode and interpret mode...).
So what you have to do is bring up your browser and type the URL into the Location box. The URL you should type will be like this one:
http://student.cs.ucc.ie/~dtab1/cs1102/lab10/fleas.php
Obviously, you use your own user id in place of dtab1
.
If all is well, you poem will appear in the browser. (Don't worry if it all appears on a single line.)
If instead an error message appears, check fleas.php
. (Hint 1:
semicolons? Hint 2: that apostrophe?) If the hints don't solve it, ask for help.
If all is well, use your browser to view the source.
Sorry if you feel insulted by how easy the programming is, but I really want you to practice running PHP scripts, so that it becomes second-nature. In which case...
In your lab10
folder, create a file called header.html
(or header.htm
), containing
only the following
<h1>Lab10 Poems</h1>
Now create two more files (by copying template.php
). Call them poem1.php
and poem2.php
.
In poem1.php
use PHP's include
command to include header.html
(or header.htm
, if that's what you called it),
and use the echo
command to output the following poem as an unordered list:
Similarly, include header.html
(or header.htm
) in poem2.php
and the following poem as an ordered list:
Run poem1.php
and poem2.php
, like you did fleas.php.
I'm not going to spell out all the steps you need for this one - you should be getting the hang of them by now.
This one is about dice. You need the following fact: the values shown on opposite faces of a die sum to 7. For example, 1 and 6 are on opposite faces.
rand
is a built-in function. Suppose m and n are integers,
then rand(m, n)
will return a randomly-chosen
integer between m and n inclusive.
Write a PHP script called dice.php
.
In the script, use rand
to simulate the roll of a die.
For the sake of example, suppose it generates the number 2.
Use echo
to put the following two paragraphs into
the output (obviously use the number generated by rand
):
Your rolled a 2
On the opposite side to a 2 is a 5
Run it several times to make sure it works.