Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Lab # 05: Programming in PHP

Spring 2024
Database Management System
Submitted by: Sibgha Noor
Registration No. 21PWCSE1975
Section: C

“On my honor, as student of University of Engineering and Technology, I have neither


given nor received unauthorized assistance on this academic work.”

Student Signature: ________________

Submitted to:
Mam Summayea Salahudin
DATED: 17/ May/ 2024

Department of Computer Systems Engineering


University of Engineering and Technology, Peshawar
Lab # 05: Programming in PHP

OBJECTIVES OF THE LAB


------------------------------------------------------------------------------------------------------------
This lab aims at the understanding of:

• PHP Basics
• PHP Data Types
• PHP Expressions
• PHP Operators
• PHP Conditionals
• PHP Loops
----------------------------------------------------------------------------------------------------

---------------------------Task 5.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 unaffected 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.
---------------------------Task 5.2---------------------------
Use and Specify the purpose of following functions:
1) intval( value )
2) floatval( value )
3) strval( value )
code:

<!DOCTYPE html>
<html>
<body>

<h1>Task 5.2</h1>

<?php
$value1 = "100";
$value2 = 3.14;
$value3 = true;

$int_value1 = intval($value1);
echo "Integer value of $value1 is $int_value1 <br>";

$float_value2 = floatval($value2);
echo "Float value of $value2 is $float_value2 <br>";

$str_value3 = strval($value3);
echo "String value of $value3 is $str_value3 <br>";
?>

</body>
</html>

output:
---------------------------Task 5.3---------------------------
Write PHP script that shows the division table displayed 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. Display this information within an HTML table.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Division Table</title>
<style>
table {
border-collapse: collapse; width: 100%;
}
th, td {
text-align: left; padding: 8px;
}
tr:nth-child(even) {background-color: #f2f2f2}
</style>
</head>
<body>
<?php
$limit = 10; // Change limit to set the table size
$isPrime = array();

echo "<h3>Division Table (Up to $limit)</h3>";


echo "<table>
<tr>
<th></th>";

// Display column headers from 1 to $limit


for ($i=1; $i <= $limit; $i++) {
echo "<th>$i</th>";
}
echo "</tr>";

for ($i = 1; $i <= $limit; $i++) {


echo "<tr>";
echo "<td>$i</td>";
for ($j = 1; $j <= $limit; $j++) {
$result = $i / $j;
$prime = isPrime($result) ? ' (Prime)' : '';
$oddEven = ($result % 2 == 0) ? ' (Even)' : ' (Odd)';
echo "<td>" . number_format($result, 3) . "$prime$oddEven</td>";
}
echo "</tr>";
}

echo "</table>";

// Function to check if a number is prime


function isPrime($num) {
if ($num <= 1) return false;
if ($num <= 3) return true;
if ($num % 2 == 0 || $num % 3 == 0) return false;
for ($i = 5; $i * $i <= $num; $i += 6) {
if ($num % $i == 0 || $num % ($i + 2) == 0) {
return false;
}
}
return true;
}
?>
</body>
</html>

Output:
---------------------------Task 5.4---------------------------
Explore PHP Object Oriented using examples showing classes, objects, inheritance, and
polymorphism.
Code:
Classes:
<?php

class Mobile {
public $brand;
public $model;
public $price;

public function __construct($brand, $model, $price) {


$this->brand = $brand;
$this->model = $model;
$this->price = $price;
}

public function displayInfo() {


echo "Brand: {$this->brand}, Model: {$this->model}, Price: {$this-
>price}$<br>";
}
}
$iphone = new Mobile("Apple", "iPhone 12", 999);
$samsung = new Mobile("Samsung", "Galaxy S21", 899);

$iphone->displayInfo();
$samsung->displayInfo();

?>

Inheritance:
<?php

class Person {
public $name;
public $age;

public function __construct($name, $age) {


$this->name = $name;
$this->age = $age;
}

public function displayInfo() {


echo "Name: {$this->name}, Age: {$this->age}<br>";
}
}

class Student extends Person {


public $studentID;

public function __construct($name, $age, $studentID) {


parent::__construct($name, $age);
$this->studentID = $studentID;
}

public function displayInfo() {


parent::displayInfo();
echo "Student ID: {$this->studentID}<br>";
}
}

$student1 = new Student("Sibgha Noor", 20, "21pwcse1975");


$student1->displayInfo();

?>

Polymorphism:\
<?php

abstract class Car {


protected $name;

public function __construct($name) {


$this->name = $name;
}

abstract public function drive();


}

class Sedan extends Car {


public function drive() {
echo "{$this->name} sedan is driving smoothly.<br>";
}
}

class SUV extends Car {


public function drive() {
echo "{$this->name} SUV is driving on rough terrains.<br>";
}
}

// Polymorphism in action
$cars = array(
new Sedan("Toyota"),
new SUV("Jeep")
);

foreach ($cars as $car) {


$car->drive();
}

?>
Output:
Classes:

Inheritance:

Polymorphism:

---------------------------Task 5.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 website 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">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
.gallery {
display: flex;
flex-wrap: wrap;
}
.gallery img {
width: 200px;
height: 200px;
margin: 10px;
cursor: pointer;
}
#myModal img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>

<div class="container">
<h2>Image Gallery</h2>
<div class="gallery">
<?php
// Directory containing images
$directory = "images/";

// Scan the directory for images


$images = glob($directory . "*.{jpg,jpeg,png,gif}", GLOB_BRACE);

// Loop through images and display them


foreach ($images as $image) {
echo '<img src="' . $image . '" alt="Image" class="img-thumbnail"
onclick="showImage(\'' . $image . '\')">';
}
?>
</div>
</div>

<!-- Modal -->


<div id="myModal" class="modal">
<span class="close" onclick="closeModal()">&times;</span>
<img class="modal-content" id="img01">
</div>

<script>
// Function to show image in modal
function showImage(src) {
var modal = document.getElementById("myModal");
var modalImg = document.getElementById("img01");
modal.style.display = "block";
modalImg.src = src;
}

// Function to close modal


function closeModal() {
var modal = document.getElementById("myModal");
modal.style.display = "none";
}
</script>

</body>
</html>

Output:

---------------------------Task 5.6---------------------------
Tutorial on PHP MVC Framework: LARAVEL.

a) Install Composer: https://getcomposer.org/download/


b) composer create-project --prefer-dist laravel/laravel testProject
e)

Next create new database in MySQL:


---------------------------Task 5.7---------------------------
Implement the following relationship in Laravel.

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 atleast five records in the created tables.

You might also like