Lab-5
Lab-5
IN PHP
LAB # 05
Spring 2025
CSE-403L
Database Management System Lab
Submitted to:
Engr. Sumayyea Salahuddin
Friday, May 16, 2025
Unsatis- Stu-
Exemplary Acceptable Developing
factory dent
Scor
Dimen-
e out
sion of 10
10 8 6 4 Mark
s
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 mathematical operations that re-
quire integer values. It's particularly useful when
dealing with data retrieved from sources where the
type might not be strictly controlled, such as user in-
put 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, con-
verting 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 retain their decimal compo-
nents. 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 values 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 errors.
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 us-
ing numeric data in HTML or other text-based for-
mats.
Task 3: Write PHP script that shows the division table dis-
played as in Table 5.2 using different 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. Dis-
play 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}";
}
</table>
</body>
</html>
Output:
Task 4: Explore PHP Object Oriented using examples showing
classes, objects, inheritance, and polymorphism.
// 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;
}
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>5.5: Image Gallery</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstra
p.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-tar-
get="#imageModal" data-bs-image="' . $image . '">';
echo '<img src="' . $image . '" class="img-thumbnail">';
echo '</a>';
echo '</div>';
echo '</div>';
}
?>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/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.getElement-
ById('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' => 'Re-
cruiter', 'dID' => 1],
['empID' => 2, 'empName' => 'Bob', 'empJob' => 'Mar-
keter', 'dID' => 2],
['empID' => 3, 'empName' => 'Charlie', 'empJob' => 'Soft-
ware Engineer', 'dID' => 3],
['empID' => 4, 'empName' => 'David', 'empJob' => 'Sales
Representative', 'dID' => 4],
['empID' => 5, 'empName' => 'Eva', 'empJob' => 'Accoun-
tant', 'dID' => 5]
];
displayEmployees($employees, $departments);
?>
</body>
</html>
Output: