PHP Assignment
PHP Assignment
LTD
FACULTY OF INFORMATION TECHNOLOGY
Moderator’s Comments:
1
TABLE OF CONTENTS PAGE#
Question 1
1.1 / 3-4
1.2 \ 4-5
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
Question 2
2.1 / 5-7
2.2 \ 7-10
2.3 / 10-11
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
2
Question 1
1.1
Screenshots:
Source Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>Question 1.1</h3>
<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
<?php
for($row=1;$row<=8;$row++)
{
echo "<tr>";
for($col=1;$col<=8;$col++)
3
{
$total=$row+$col;
if($total%2==0)
{
echo "<td height=30px width=30px bgcolor=#FFFFFF></td>";
}
else
{
echo "<td height=30px width=30px bgcolor=#000000></td>";
}
}
echo "</tr>";
}
?>
</table>
</body>
</html>
1.2
Screenshot:
Source Code:
<?php
echo 'Question 1.2';
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$p = $col * $row;
echo "<td>$p</td> \n";
}
4
echo "</tr>";
}
echo "</table>";
?>
Question 2
2.1
Screenshot:
Source Code:
<!DOCTYPE html>
<html>
<body>
<table border = "1rem" cellpadding="5px">
<?php
$cars = array
(
array(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2)
5
) ,
array(
array("Land Rover",17,15),
array("Range Rover",16,14),
array("Nissan",27,25)
),
array(
array("Hundai",8,3),
array("Kia",7,1),
array("Jeep",27,15),
array("Benz",8,5)
)
);
for($l=0;$l<3;$l++){
echo "<tr>";
for($r=0;$r<3;$r++){
for($c=0;$c<3;$c++){
echo "<td>".$cars[$l][$r][$c]."</td>";
}
echo "</tr>";
}
}
?>
</table>
</body>
</html>
2.2
WHAT IS A CONTROL STRUCTURE?
Code execution can be grouped into categories as shown below
Sequential – this one involves executing all the codes in the order in which they have been
written.
Decision – this one involves making a choice given a number of options. The code executed
depends on the value of the condition.
6
A control structure is a block of code that decides the execution path of a program
depending on the value of the set condition.
PHP IF Else
If… then... else is the simplest control structure. It evaluates the conditions using Boolean
logic
When to use if… then… else
You have a block of code that should be executed only if a certain condition is true
You have two options, and you have to select one.
If… then… else if… is used when you have to select more than two options and you
have to select one or more
<?php
if (condition is true) {
block one
else
block two
}
?>
HERE,
<?php
$first_number = 7;
$second_number = 21;
7
echo "$first_number is greater than $second_number";
}else{
?>
Switch… case is similar to the if then… else control structure. It only executes a single block
of code depending on the value of the condition. If no condition has been met, then the
default block of code is executed. It has the following basic syntax.
<?php
switch(condition){
case value:
break;
case value2:
break;
default:
break;
}
?>
HERE,
8
“if (condition is true)” is the control structure
“block one” is the code to be executed if the condition is true
{…else…} is the fallback if the condition is false
“block two” is the block of code executed if the condition is false
<?php
$today = "wednesday";
switch($today){
case "sunday":
break;
case "wednesday":
break;
case "saturday":
break;
default:
break;
?>
Summary
9
The if then... else is when you have more than route block of code to execute
depending on the value of the condition
Switch… case is used to when you have a number of block codes, and you only have
to execute one of them depending on the value of the set case
2.3
Screenshot:
Source code:
<?php
/*The student names*/
$students = ["Sauer Jeppe","Von Weiligh","Troy Commisioner","Paul
krugger","Jacob Maree"];
/* The students marks*/
$marks = [75,44,60,62,70];
switch($marks){
case $marks >= 70 : echo "$students[0] scored a distinction";
echo "<br>";
echo "$students[4] scored a distinction";
echo "<br>";
case $marks >= 50 && $marks<69 : echo "$students[2] scored a pass";
echo "<br>";
echo "$students[3] scored a pass";
echo "<br>";
case $marks < 49 : echo "$students[1] scored a fail";
};
?>
10
11