You are on page 1of 3

Web Development

Lab 3: Basic PHP & Javascript

Objective: The purpose of this lab is to explore PHP and to learn how to use the PHP
programming language to solve problems and to use it to fulfill classic web functions such as
forms treatment.

Exercise 1: Conditional statements


1. Write a conditional statement using the variables $age and $sex in an if instruction to
select a female person whose age is between 20 and 40 then display the following
message if the person qualifies for these conditions:
Hello Madam, your have the adequate profile for the mission.
And the following message if the person does not qualify for the conditions:
Sorry, your profile is inadequate for this mission.

Exercise 2: Looping statements


1. Write a PHP script that performs random choices of numbers until obtaining a sequence
of an even number followed by two odd numbers. To do so, use the do while
conditional statement.
You can use the PHP internal function rand (http://php.net/manual/en/function.rand.php)
to generate the random numbers.
The random numbers can range from 0 to 1000.

Exercise 3: Strings
1. Using the strlen() function, write a loop that displays each letter of a string (of your
choice) on a separate line.
2. Write a PHP script that checks if a string contains the word zut. If the string
contains the word zut then the following should be displayed The string contains a
bad word. And if it does not contain it the string in question should be written in
bold.
You can use the PHP function eregi() is this exercise.

Exercise 4: Arrays
1. Write a PHP script that creates an array containing email addresses. You can use the
following email addresses as an example:
php@free.com
php2@free.com
slimshady@yahoo.com
yoyoyo@gmail.com
yoyo@hotmail.com
xoxo@live.com
cs220@gmail.com
cs220@yahoo.com
cs220@free.com
gg@gmail.com
tbs@tbs.com.tn
2. Extract the domain names from the email addresses and put them in a separate array.
Each domain must have only one occurrence in this new array.
You may use the foreach() construct to work on the original array and the
explode() function to extract the domain name from the email adres.
3. Write down the statistics of the usage of each domain name in the original array
following this example:
The domain name free.com = 23.5 %
The domain name gmail.com = 16.1%

Exercise 5: Forms and PHP


1. Create an HTML form that contains text fields asking clients to enter their first name, last
name, address, town and the zip code. The form should look like this:
The data is then treated by a separate PHP file that displays the values entered by the
client in a table like the image below:

2. Add a Javascript to the PHP script that displays the entered data only if all the fields
contain values. If there is at least one of the fields that is empty, the Javascript should
display an alert box saying One or more fields are empty. Please check your
coordinates..

You might also like