PROGRAMMING
IN PHP
LAB # 05
Spring 2024
CSE-403L
Database Management System Lab
Submitted by: Anees Ur Rehman
Registration No.: 22PWCSE2168
Class Section: ‘B’
“I affirm that I have completed this work with integrity”
Student Signature: ______________
Submitted to:
Engr. Sumayyea Salahuddin
Friday, May 16, 2025
Department of Computer Systems Engineering
University of Engineering and Technology, Peshawar
Database Management Systems Lab
LAB ASSESSMENT RUBRICS
Unsatisfac- Stu-
Exemplary Acceptable Developing
tory dent
Score
Dimension out of
10 8 6 4 10
Marks
Overall Report is complete, Report is com- Report is mostly Report is in-
Impression of well written, and orga- plete, briefly writ- complete, loosely complete,
Lab Report nized appropriately ten, and organized. written, and fairly sloppy and/or
with additional ele- There are few organized. There disorganized.
ments that enhance it. spellings and/or are spellings and/ There are many
There are no spellings grammar errors. or grammar errors. spellings and
or grammar errors. grammar errors
that affect clar-
ity.
Submission Report is submitted on Report is submit- Report is submit- Report was
time. ted within 24 ted within 72 more than 3
hours of due date. hours of due date. days overdue.
Specification Programs work and ex- Programs work Programs work Programs work
ceed specifications. and meet all speci- and meet partial but fail to meet
fications. specifications. any specifica-
tions.
Verbal Answered clearly and Answered clearly Answered some- Answered
Communica- accurately with suffi- and accurately what clearly and wrongly and in-
tion and cient knowledge. with average somewhat accu- accurately with
Understand- knowledge. rately with limited no knowledge.
ing knowledge.
Output Fig- All the output figures Most of the output Few of the output Output figures
ures/Graphics and graphics are shown figures and graph- figures and graph- and graphics are
clearly and labeled. ics are shown ics are shown shown clearly
clearly and la- clearly and la- and not labeled.
beled. beled.
Marks: (_______+_______+_______+_______)/5= _______
Teacher Remarks and Signature: ____________________________________________________
Lab Tasks:
Task 1: In contrast to settype(), there is another method that causes a variable’s value to be
treated as a specific type. It is known as type casting. Note that the variable itself remains un-
affected during type casting. Consider the following variable:
$test_var = 8.23;
Type cast this variable’s value to integer, string, and Boolean and show result using echo.
Code:
<?php
$test_var = 8.23;
// Casting to integer
$int_var = (int)$test_var;
echo "Integer: " . $int_var . "<br>";
// Casting to string
$str_var = (string)$test_var;
echo "String: " . $str_var . "<br>";
// Casting to Boolean
$bool_var = (bool)$test_var;
echo "Boolean: " . ($bool_var ? 'true' : 'false') . "<br>";
?>
Output:
Task 2: Use and specify the purpose of following functions:
intval(value )
floatval(value )
strval(value )
1. intval(value)
Purpose: Converts a given value to an integer.
Usage: This function is commonly used when you need to perform mathemati-
cal operations that require integer values. It's particularly useful when dealing
with data retrieved from sources where the type might not be strictly con-
trolled, such as user input or database calls.
Example: If you have a string '123.45' and you use intval('123.45'), the result
will be 123. This function also handles other types like Booleans and nulls,
converting true to 1 and false or null to 0.
2. floatval(value)
Purpose: Converts a given value to a floating point number (float).
Usage: This is useful when you need to ensure that operations on numbers re-
tain their decimal components. It is particularly important when calculations
require precision, such as in financial calculations or scientific computations.
Example: When converting the string '123.45' with floatval('123.45'), the
output will be 123.45 as a float.
3. strval(value)
Purpose: Converts a given value to a string.
Usage: This function is essential when you need to concatenate numerical val-
ues with strings or when you need to output data as part of a text. It ensures
that numeric values can be easily appended to text strings without causing er-
rors.
Example: If you have an integer 123, using strval(123) will convert it to the
string '123'. This is helpful for outputting numbers within text or for using nu-
meric data in HTML or other text-based formats.
Task 3: Write PHP script that shows the division table displayed as in Table 5.2 using dif-
ferent loops. For each number, display whether that number is an odd or even number, and
also display a message if the number is a prime number. Display this information within an
HTML table.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>5.3: Division Table</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
text-align: center;
}
</style>
</head>
<body>
<h1>Task 5.3: Division Table</h1>
<table>
<tr>
<th>/</th>
<?php for ($i = 1; $i <= 10; $i++): ?>
<th><?= $i ?></th>
<?php endfor; ?>
</tr>
<?php
function is_prime($num) {
if ($num < 2) return false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) return false;
}
return true;
}
function classify_number($num) {
$type = fmod($num, 2) == 0 ? "even" : "odd";
$prime = is_prime($num) ? "and prime" : "";
return "{$type} {$prime}";
}
for ($row = 1; $row <= 10; $row++) {
echo "<tr>";
echo "<th>$row</th>";
for ($col = 1; $col <= 10; $col++) {
$result = $row / $col;
// Check if the result is an integer, if so, classify it
if (intval($result) == $result) {
$description = classify_number($result);
} else {
$description = ""; // Do not classify non-integers
}
$formattedResult = round($result, 3);
echo "<td>{$formattedResult} {$description}</td>";
}
echo "</tr>";
}
?>
</table>
</body>
</html>
Output:
Task 4: Explore PHP Object Oriented using examples showing classes, objects, inheritance,
and polymorphism.
Code for Class:
<?php
class Car {
// Properties
public $color;
public $model;
// Constructor
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
// Method
public function getDetails() {
return $this->color . " " . $this->model;
}
}
// Creating an object
$myCar = new Car("red", "Toyota");
echo $myCar->getDetails(); // Outputs: red Toyota
?>
Code for Inheritance:
<?php
class Vehicle {
public $brand;
public function __construct($brand) {
$this->brand = $brand;
}
public function start() {
return "Starting the engine...";
}
}
class Motorcycle extends Vehicle {
public function wheelie() {
return "Performing a wheelie!";
}
}
$motorcycle = new Motorcycle("Harley-Davidson");
echo $motorcycle->start(); // Outputs: Starting the engine...
echo $motorcycle->wheelie(); // Outputs: Performing a wheelie!
?>
Code for Polymorphism:
<?php
interface Animal {
public function makeSound();
}
class Dog implements Animal {
public function makeSound() {
return "Bark";
}
}
class Cat implements Animal {
public function makeSound() {
return "Meow";
}
}
function animalSound(Animal $animal) {
echo $animal->makeSound();
}
$dog = new Dog();
$cat = new Cat();
animalSound($dog); // Outputs: Bark
animalSound($cat); // Outputs: Meow
?>
Task 5: Build an image gallery website using PHP without database. Your website must
show all the images in a given directory. Use bootstrap/CSS in your website. Please note that
when a given image is clicked, it enlarges it and shows in higher resolution. Also, your web-
site should be flexible enough to show the images when number of images are increased or
decreased from a given directory.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>5.5: Image Gallery</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet">
<style>
.gallery img {
width: 100%;
height: auto;
padding: 5px;
}
.modal-body img {
width: 100%;
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="row">
<?php
$dir = "../images/";
$images = glob($dir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach ($images as $image) {
echo '<div class="col-lg-3 col-md-4 col-sm-6 mb-4">';
echo '<div class="gallery">';
echo '<a href="#" data-bs-toggle="modal" data-bs-target="#imageModal" data-bs-
image="' . $image . '">';
echo '<img src="' . $image . '" class="img-thumbnail">';
echo '</a>';
echo '</div>';
echo '</div>';
}
?>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="imageModal" tabindex="-1" aria-labelledby="imageModal-
Label" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="imageModalLabel">Image Preview</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-la-
bel="Close"></button>
</div>
<div class="modal-body">
<img src="" id="modalImage" alt="Image preview">
</div>
</div>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
var imageModal = document.getElementById('imageModal');
imageModal.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget;
var src = button.getAttribute('data-bs-image');
var modalImage = document.getElementById('modalImage');
modalImage.src = src;
});
</script>
</body>
</html>
Output:
Task 7: Implement the following relationship in PHP.
Department: depID, depName
Emplyee: empID, empName, empJob, dID
Note that the department has at least one or many employees working in it; while employee
works exactly in one department. Also, feed at least five records in the created tables.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>7: Company Structure</title>
</head>
<body>
<h1>Employees Information</h1>
<?php
$departments = [
['depID' => 1, 'depName' => 'Human Resources'],
['depID' => 2, 'depName' => 'Marketing'],
['depID' => 3, 'depName' => 'Engineering'],
['depID' => 4, 'depName' => 'Sales'],
['depID' => 5, 'depName' => 'Finance']
];
$employees = [
['empID' => 1, 'empName' => 'Alice', 'empJob' => 'Recruiter', 'dID' => 1],
['empID' => 2, 'empName' => 'Bob', 'empJob' => 'Marketer', 'dID' => 2],
['empID' => 3, 'empName' => 'Charlie', 'empJob' => 'Software Engineer', 'dID' => 3],
['empID' => 4, 'empName' => 'David', 'empJob' => 'Sales Representative', 'dID' => 4],
['empID' => 5, 'empName' => 'Eva', 'empJob' => 'Accountant', 'dID' => 5]
];
function getDepartmentName($departments, $depID) {
foreach ($departments as $department) {
if ($department['depID'] == $depID) {
return $department['depName'];
}
}
return "Department not found";
}
function findEmployeesByDepartment($employees, $depID) {
$deptEmployees = [];
foreach ($employees as $employee) {
if ($employee['dID'] == $depID) {
$deptEmployees[] = $employee;
}
}
return $deptEmployees;
}
function displayEmployees($employees, $departments) {
foreach ($employees as $employee) {
$departmentName = getDepartmentName($departments, $employee['dID']);
echo "Name: " . $employee['empName'] . ", Job: " . $employee['empJob'] . ", De-
partment: " . $departmentName . "<br>";
}
}
displayEmployees($employees, $departments);
?>
</body>
</html>
Output: