程序代写代做代考 COMP284 Practical 4

COMP284 Practical 4
PHP (1)

Introduction

• This worksheet contains exercises that are intended to familiarise you with PHP Program-
ming. While you work through the exercises below compare your results with those of
your fellow students and ask for help and comments if required.

• This document can be found at

http://cgi.csc.liv.ac.uk/~ullrich/COMP284/notes/practical04.pdf

and you might proceed more quickly if you cut-and-paste code from that PDF file. Note
that a cut-and-paste operation may introduce extra spaces into your code. It is important
that those are removed and that your code exactly matches that shown in this worksheet.

• The exercises and instructions in this worksheet assume that you use the Department’s
Linux systems to experiment with PHP.

If you want to use the Department’s Windows systems instead, then you can do so.

• To keep things simple, we will just use a text editor, a terminal, and a web browser. You
can use whatever text editor and web browser you are most familiar or comfortable with.

• If you do not manage to get through all the exercises during this practical session, please
complete them in your own time before the next practical takes place.

Exercises

1. Let us start by re-creating the ‘Hello World’ PHP script that you have seen in the lectures.

It is assumed that you have already created a directory
$HOME/public_html/

in the last practical session and that the directory is both readable and executable by
everyone. Make sure that this is so before you proceed.

a. Open a text editor and enter the following PHP code:

PHP Scalars

PHP Scalars

“;
print (“

Hello $user
\nHello World!

\n”);

?>

1

http://cgi.csc.liv.ac.uk/~ullrich/COMP284/notes/practical04.pdf

Replace with your own name.

b. Save the code to a file named php04A.php in $HOME/public_html/.

c. In a terminal, go to the directory in which the file has been stored, make sure the file
php04A cannot be read, written or executed by anyone else using the command

chmod o-rwx php04A

and execute the PHP script by using the command
php ./php04A.php

in the terminal. Check that there are no syntax errors and the output is

PHP Scalars

PHP Scalarst

Hello

Hello World!

(Obviously, with your name appearing instead of .)

d. Now open a web browser and access the URL

http://cgi.csc.liv.ac.uk/∼/php04A.php

where should be replaced by your departmental user name.
Make sure that the web page you are shown corresponds to the HTML code you have
seen in Exercise 1c above.
Hint: Your web browser can shown you the HTML source of a web page.

e. It is good practice to add information about who created a script/web page, who claims
copyright and when a script/web page was created.
Add these three pieces of information to the script using META declarations. See

https://www.w3.org/TR/html52/single-page.html#the-meta-element

for details.

2. The following example deals with the use of constants in PHP code.

a. Add the following PHP code to the end of the current PHP code in php04A.php (make
sure that the additional PHP code is included before ?>):

define(“PI”,3.14159);

define(“SPEED_OF_LIGHT”,299792458,true);

print “1 – Value of PI: PI
\n”;

print “2 – Value of PI: “.PI.”
\n”;

$diameter = 2;
$time = 3;
$circumference1 = PI * $diameter;
$circumference2 = pi * $diameter;
$distance = speed_of_light * $time;
echo “Diameter = $diameter => “,

2

https://www.w3.org/TR/html52/single-page.html#the-meta-element

“Circumference1 = $circumference1 | “,
“Circumference2 = $circumference2
\n”;

echo “Time = $time => Distance = $distance
\n”;

b. Save the file and execute the script again using the web browser.

c. Check that the additional output shown in the web browser starts with the two lines

1 – Value of PI: PI

2 – Value of PI: 3.14159

and that you understand how the difference between lines 1 and 2 comes about.

d. Make sure that you understand how the constants PI and SPEED_OF_LIGHT ‘work’ and
why the output indicates that Circumference1 has value 6.28318 while Circumference2
has value 0.

e. Change your script so that the four lines of output produced by the code in Exercise 2a
appear in a red bold-face font in the web browser.

3. The following example deals with types, type casting, and the identification of types in
PHP.

a. Add the following PHP code to the end of the current PHP code in php04A.php:

$mode = rand(1,4);
if ($mode == 1)

$randvar = rand();
elseif ($mode == 2)

$randvar = (string) rand();
elseif ($mode == 3)

$randvar = rand()/(rand()+1);
else

$randvar = (bool) $mode;
echo “Random scalar: $randvar
\n”;

b. Save the file and execute the script several times using the web browser. See how the
value of $randvar changes every time that the script is executed.

c. Extend the script with code that displays

• This is a natural number if $randvar stores a natural number
• This is a floating-point number if $randvar stores a floating-point number
• This is a string if $randvar stores a string
• I don’t know what this is in any other case

after the line Random scalar: …. You are not allowed to use the value of $mode for
this purpose.
Hints: Recall from the lecture notes what gettype(), is_int(), is_string(), etc do.
Also have a look at switch statements in PHP:

http://php.net/manual/en/control-structures.switch.php

4. The following example deals with comparison operators and the generation of HTML tables
in PHP.

a. Add the following PHP code to the end of the current PHP code in php04A.php:

3

http://php.net/manual/en/control-structures.switch.php

$a = array(0,123,1.23e2,”123″,TRUE,FALSE);
echo “

\n”;

foreach ($a as $sa)
foreach ($a as $sb) {
$val_sa = gettype($sa).”(“.var_export($sa,true).”)”;
$val_sb = gettype($sb).”(“.var_export($sb,true).”)”;
echo “

“;

printf(“

“, $val_sa,$val_sb,
($sa == $sb) ? “TRUE” : “FALSE”);

printf(“

“,$val_sa,$val_sb,
($sa === $sb) ? “TRUE” : “FALSE”);

printf(“

“, $val_sa,$val_sb,
($sa != $sb) ? “TRUE” : “FALSE”);

printf(“

“,$val_sa,$val_sb,
($sa !== $sb) ? “TRUE” : “FALSE”);

echo “

“;

}

echo “

%20s == %20s -> %5s %20s === %20s -> %5s %20s != %20s -> %5s %20s !== %20s -> %5s

\n”;

b. Save the file and execute the script again using the web browser. Analyse the table
that the additional code produces and make sure that you understand its content. Also
make sure that you understand how the code above produces that table. (Generating
tables is a typical task for which PHP code is used.)

5. The following example deals with the modification of array elements, string operations and
pattern search and replace.

a. Open a text editor and enter the following PHP code:

PHP Arrays

PHP Arrays

\n”);

?>

b. Save the code to a file named php04B.php in $HOME/public_html/, adjust its access
rights as shown in Exercise 1c, then execute the script using the web browser.
The output produced by the code above should be as follows:

Name: Sebastian Coope

Name: Andrew Craig

4

Name: Prof Karl Tuyls

Name: Dr Ullrich Hustadt

Name: Dr Michele Zito

c. Add code at the point indicated in the code in Exercise 5a that modifies the strings
stored in $names so that the output produced will change to:

Name: COOPE, Sebastian

Name: CRAIG, Andrew

Name: TUYLS, Karl

Name: HUSTADT, Ullrich

NAME: ZITO, Michele

This should be done with the help of string manipulation operations, see, for example,

http://www.php.net/manual/en/function.preg-match.php

http://www.php.net/manual/en/function.preg-replace-callback.php

and the code should be independent of the actual names stored in $names.
Hints: Recall from the lecture notes (Lecture 6, Slide 5) how this was done in Perl.
Also recall from the lecture notes the variation of a foreach-loop that allows you to
modify the elements of an array.

5

http://www.php.net/manual/en/function.preg-match.php
http://www.php.net/manual/en/function.preg-replace-callback.php