Uploaded by Uday Kanwar

INFO3135-Practice2

advertisement
INFO 3135
Practice 2
Topics:
•
•
•
•
Basic PHP syntax and variables
Outputting content using echo and print
If statements, else if, and else conditions
Switch statements for multi-choice conditions
Introduction to loops: for, while, and do-while
Loop control: break and continue
Folder Name: "Practice2"
Please save all your .php files in the "Practice2" folder.
Upon downloading "Practice2", a .zip file will be generated. Ensure you submit this .zip file before the
deadline.
Problem 1 [1 mark]
Filename: p1_yourStudentid.php
You are tasked with creating a simple interest calculator using PHP. The formula for calculating
simple interest is:
Requirements:
1. Declare three variables: $principal, $rate, and $time. Assign them values of your choice.
2. Calculate the simple interest using the formula provided and store the result in a variable
named $simpleInterest.
3. Use echo or print to display the following:
a. The principal amount.
b. The rate of interest.
c. The time period.
d. The calculated simple interest.
Sample Output:
Principal Amount: $1000
Rate of Interest: 5%
Time Period: 2 years
Calculated Simple Interest: $100
Problem 2 [1 mark]
INFO 3135
Practice 2
Filename: p2_yourStudentid.php
You are tasked with creating a PHP script that determines the grade of a student based on their
score.
Requirements:
1. Declare a variable $score and assign it a value between 0 and 100.
2. Write a PHP script that checks the value of $score and assigns a grade based on the
following criteria:
- 90 and above: A
- 80 to 89: B
- 70 to 79: C
- 60 to 69: D
- Below 60: F
3. Display the grade using echo.
Sample Output:
Your score is 85. Your grade is B.
Problem 3 [1 mark]
Filename: p3_yourStudentid.php
You are developing a PHP script for a community center that classifies individuals into age groups
for different activities.
Requirements:
1. Declare a variable $age and assign it a value.
2. Write a PHP script that checks the value of $age and classifies the individual into one of the
following age groups:
- Below 13: Child
- 13 to 19: Teenager
- 20 to 35: Young Adult
- 36 to 55: Adult
- Above 55: Senior
3. If the age is negative or above 120, display an error message.
4. Display the age group using echo.
Sample Output:
You are 25 years old. You belong to the Young Adult age group.
Error Sample Output:
Invalid age provided.
Problem 4 [2 marks]
INFO 3135
Practice 2
Filename: p4_yourStudentid.php
You are tasked with creating a PHP script that provides a brief description of each day of the
week.
Requirements:
1. Declare a variable $dayNumber and assign it a value between 1 and 7.
2. Use a switch statement to determine which day of the week the number corresponds to and
provide a brief description of that day.
3. If the number is outside the range of 1 to 7, display an error message.
4. Display the day and its description using echo.
Descriptions:
-
Monday (1): Start of the workweek.
Tuesday (2): Second day of the workweek.
Wednesday (3): Midweek day.
Thursday (4): Almost the weekend.
Friday (5): Last day of the workweek.
Saturday (6): Weekend relaxation.
Sunday (7): Day of rest before the new week.
Sample Output:
You chose 4. Thursday: Almost the weekend.
Error Sample Output:
Invalid day number provided.
Problem 5 [2 marks]
INFO 3135
Practice 2
Filename: p5_yourStudentid.php
You are tasked with creating a PHP script that displays numbers from 1 to 50. However, there's a
twist: you should skip all even numbers between 20 and 40.
Requirements:
1. Use a for loop to iterate through numbers from 1 to 50.
2. Within the loop, implement a condition to check if the current number is even and falls
between 20 and 40.
3. If the above condition is met, use the continue statement to skip the current iteration.
4. Display each number (that isn't skipped) using echo, separated by a comma.
Expected Output: (It can be displayed horizontally or vertically. Your decision.)
1, 2, 3, ..., 19, 21, 23, ..., 39, 41, 42, ..., 50
Note: The numbers between 20 and 40 that are even (i.e., 20, 22, 24, ..., 40) should not appear
in the output.
Problem 6 [3 marks]
Filename: p6_yourStudentid.php
You have been given an associative array of employees. Each employee has a name, position,
and department. Your task is to display this list of employees in an HTML table using the foreach
loop.
Given Data:
$employees = array(
array("name" => "John Doe", "position" => "Software Engineer", "department" => "IT"),
array("name" => "Jane Doe", "position" => "HR Manager", "department" => "Human
Resources"),
array("name" => "Butter Doe", "position" => "Accountant", "department" => "Finance"),
);
Requirements:
1. Use a foreach loop to iterate through each employee in the $employees array.
INFO 3135
Practice 2
2. Display the list of employees in an HTML table with columns for "Name", "Position", and
"Department".
3. Each row of the table should represent an employee and their details.
Expected Output:
<table border="1">
<tr>
<th>Name</th>
<th>Position</th>
<th>Department</th>
</tr>
<tr>
<td>John Doe</td>
<td>Software Engineer</td>
<td>IT</td>
</tr>
<!—display the rest of the data -->
</table>
$employees = array(
// the data given
);
echo '<table border="1">';
echo '<tr>
<th>Name</th>
<th>Position</th>
<th>Department</th>
</tr>';
foreach ($employees as $employee) {
// create a row for each employee and column for each field
}
echo '</table>';
Download