0% found this document useful (0 votes)
6 views

PHP Practical File

PHP gshjeb Ududje Shidjhdmd Hhhjjxbdhdnsmnrjfm Dhrjrj F Urijrhrjdjdjrnfjjd Pptitjhdhdhhhhhxhdbdjrhdhd ok by I'm going to be will be will be will yyyyyy ok by I'm going to be will be will be will be will be will be will be will be will yyyyyy you have any iiiiiiiiiiiiiiiiiiiii ok

Uploaded by

bunnyik6264
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PHP Practical File

PHP gshjeb Ududje Shidjhdmd Hhhjjxbdhdnsmnrjfm Dhrjrj F Urijrhrjdjdjrnfjjd Pptitjhdhdhhhhhxhdbdjrhdhd ok by I'm going to be will be will be will yyyyyy ok by I'm going to be will be will be will be will be will be will be will be will yyyyyy you have any iiiiiiiiiiiiiiiiiiiii ok

Uploaded by

bunnyik6264
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

A&M Institutes Of Management &

Technology,Pathankot

Practical File
Programming in PHP
UGCA 1930

Submitted by: Submitted to:


Sambhav Chadha Mr. Bhuvnesh Pathania
2208468
2
INDEX

SR. PRACTICALS PAGE TEACHERS


NO NO. SIGNATURE
Take values from the user and
1. compute sum, subtraction,
4
multiplication, division and
exponent of value of the
variables.

2. Write a program to find 6


area of following shapes:
circle, rectangle, triangle,
square, trapezoid and
parallelogram.
3. Compute and print roots 11
of quadratic equation.
4. Write a program to 14
determine whether a
triangle is isosceles or not
5. Print multiplication table 16
of a number input by the
user
6. Calculate sum of natural 19
numbers from one to n
number.
7. Print Fibonacci series up 21
to n numbers e.g. 0 1 1 2
3 5 8 13 21…..n
8. Write a program to find 23
the factorial of any
number
9. Determine prime 25
numbers within a specific
range
10. Write a program to 27
compute, the Average
and Grade of students
marks.
3

11. Compute addition, 30


subtraction and
multiplication of a matrix
12. Count total number of 34
vowels in a word “Develop
& Empower Individuals”.
13. Determine whether a string 36
is palindrome or not?

14. Display word after Sorting in 38


alphabetical order.

15. Check whether a number is 40


in a given range using
functions.

16. Write a program accepts a 42


string and calculates
number of upper case
letters and lower case
letters available in that
string.
17. Design a program to reverse 44
a string word by word

18. Write a program to create a 46


login form. On submitting
the form, the user should
navigate to profile page.
19. Design front page of a 49
college or department using
graphics method.

20. Write a program to upload 54


and download files
4

Practical 1
Take values from the user and compute sum, subtraction, multiplication, division and
exponent of value of the variables.

<?php
if(isset($_POST['disp']))
{
$f=$_POST['f'];
$s=$_POST['s'];
$ch=$_POST['ch'];
switch($ch)
{
case 'ADDITION':
$res=$f+$s;
break;
case 'SUBTRACTION':
$res=$f-$s;
break;
case 'MULTIPLICATION':
$res=$f*$s;
break;
case 'DIVISION':
$res=$f/$s;
break;
}
}
?>
<html>
<body bgcolor="gold">
<form action="" method="post">
<table align="center" border="3" width="20%"><br><br>
<tr bgcolor="forestgreen"><td align="center" colspan="2">
<font color="white" face="arial black" size="5">CALCULATOR</font></td></tr>
<tr><td><input type="button" value="Enter First Input "></td>
<td><input type="text" name="f"></td></tr>
<tr><td><input type="button" value="Enter Second Input"></td>
<td><input type="text" name="s"></td></tr>
<tr><td><input type="button" value="Select Your Choice"></td>
<td><center><select name="ch">
<option>ADDITION</option>
<option>SUBTRACTION</option>
<option>MULTIPLICATION</option>
<option>DIVISION</option>
</center></select></td></tr>
<tr><td><input type="submit" value=" RESULT " name="disp"></td>
<td><input type="text" value="<?php echo @$res; ?>" readonly="true"/>
</td></tr></table>
</body></html>
5

OUTPUT
6

Practical 2
Write a program to find area of following shapes: circle, rectangle, triangle, square,
trapezoid and parallelogram.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Area Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #e0f7fa;
color: #333;
margin: 0;
padding: 20px;
}
h1 {
text-align: center;
color: #00796b;
}
.shape-buttons {
text-align: center;
margin-bottom: 20px;
}
.shape-buttons button {
padding: 10px 20px;
margin: 10px;
border: none;
border-radius: 5px;
background-color: #00796b;
color: white;
font-size: 16px;
cursor: pointer;
}
.shape-buttons button:hover {
background-color: #004d40;
}
form {
background: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
max-width: 600px;
margin: 20px auto;
display: none; /* Hidden by default */
}
label {
display: block;
7

margin: 10px 0 5px;


font-weight: bold;
}
input[type="number"], input[type="submit"] {
padding: 8px;
margin: 5px 0;
border-radius: 4px;
border: 1px solid #ccc;
width: calc(100% - 20px);
}
input[type="submit"] {
background-color: #00796b;
color: white;
border: none;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #004d40;
}
.result {
text-align: center;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<h1>Area Calculator</h1>

<div class="shape-buttons">
<button onclick="showForm('circle')">Circle</button>
<button onclick="showForm('rectangle')">Rectangle</button>
<button onclick="showForm('triangle')">Triangle</button>
<button onclick="showForm('square')">Square</button>
<button onclick="showForm('trapezoid')">Trapezoid</button>
<button onclick="showForm('parallelogram')">Parallelogram</button>
</div>

<?php
$area = null;
$shape = '';
$radius = $length = $width = $triangle_base = $triangle_height = $side = $base1 = $base2 =
$trapezoid_height = $parallelogram_base = $parallelogram_height = 0;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$shape = $_POST['shape'] ?? '';
$radius = $_POST['radius'] ?? 0;
$length = $_POST['length'] ?? 0;
$width = $_POST['width'] ?? 0;
$triangle_base = $_POST['triangle_base'] ?? 0;
$triangle_height = $_POST['triangle_height'] ?? 0;
$side = $_POST['side'] ?? 0;
8

$base1 = $_POST['base1'] ?? 0;
$base2 = $_POST['base2'] ?? 0;
$trapezoid_height = $_POST['trapezoid_height'] ?? 0;
$parallelogram_base = $_POST['parallelogram_base'] ?? 0;
$parallelogram_height = $_POST['parallelogram_height'] ?? 0;

switch ($shape) {
case 'circle':
$area = pi() * pow($radius, 2);
break;
case 'rectangle':
$area = $length * $width;
break;
case 'triangle':
$area = 0.5 * $triangle_base * $triangle_height;
break;
case 'square':
$area = pow($side, 2);
break;
case 'trapezoid':
$area = 0.5 * ($base1 + $base2) * $trapezoid_height;
break;
case 'parallelogram':
$area = $parallelogram_base * $parallelogram_height;
break;
}
}
?>

<form method="post" id="shape-form">


<input type="hidden" name="shape" id="shape-type">
<div id="circle-fields" class="shape-fields hidden">
<label for="radius">Radius:</label>
<input type="number" name="radius" id="radius" step="any">
</div>
<div id="rectangle-fields" class="shape-fields hidden">
<label for="length">Length:</label>
<input type="number" name="length" id="length" step="any">
<label for="width">Width:</label>
<input type="number" name="width" id="width" step="any">
</div>
<div id="triangle-fields" class="shape-fields hidden">
<label for="triangle_base">Base:</label>
<input type="number" name="triangle_base" id="triangle_base" step="any">
<label for="triangle_height">Height:</label>
<input type="number" name="triangle_height" id="triangle_height" step="any">
</div>
<div id="square-fields" class="shape-fields hidden">
<label for="side">Side:</label>
<input type="number" name="side" id="side" step="any">
</div>
<div id="trapezoid-fields" class="shape-fields hidden">
<label for="base1">Base 1:</label>
9

<input type="number" name="base1" id="base1" step="any">


<label for="base2">Base 2:</label>
<input type="number" name="base2" id="base2" step="any">
<label for="trapezoid_height">Height:</label>
<input type="number" name="trapezoid_height" id="trapezoid_height" step="any">
</div>
<div id="parallelogram-fields" class="shape-fields hidden">
<label for="parallelogram_base">Base:</label>
<input type="number" name="parallelogram_base" id="parallelogram_base" step="any">
<label for="parallelogram_height">Height:</label>
<input type="number" name="parallelogram_height" id="parallelogram_height" step="any">
</div>
<input type="submit" value="Calculate">
</form>

<?php if ($area !== null): ?>


<div class="result">
<h2>Calculated Area</h2>
<p>The area of the <?= htmlspecialchars(ucfirst($shape)) ?> is: <strong><?=
number_format($area, 2) ?> square units</strong></p>
<p><strong>Input Values:</strong></p>
<ul>
<?php if ($shape == 'circle'): ?>
<li>Radius: <?= htmlspecialchars($radius) ?></li>
<?php elseif ($shape == 'rectangle'): ?>
<li>Length: <?= htmlspecialchars($length) ?></li>
<li>Width: <?= htmlspecialchars($width) ?></li>
<?php elseif ($shape == 'triangle'): ?>
<li>Base: <?= htmlspecialchars($triangle_base) ?></li>
<li>Height: <?= htmlspecialchars($triangle_height) ?></li>
<?php elseif ($shape == 'square'): ?>
<li>Side: <?= htmlspecialchars($side) ?></li>
<?php elseif ($shape == 'trapezoid'): ?>
<li>Base 1: <?= htmlspecialchars($base1) ?></li>
<li>Base 2: <?= htmlspecialchars($base2) ?></li>
<li>Height: <?= htmlspecialchars($trapezoid_height) ?></li>
<?php elseif ($shape == 'parallelogram'): ?>
<li>Base: <?= htmlspecialchars($parallelogram_base) ?></li>
<li>Height: <?= htmlspecialchars($parallelogram_height) ?></li>
<?php endif; ?>
</ul>
</div>
<?php endif; ?>

<script>
function showForm(shape) {
// Hide all shape fields initially
document.querySelectorAll('.shape-fields').forEach(function(field) {
field.classList.add('hidden');
});

// Show the form


document.getElementById('shape-form').style.display = 'block';
10

// Show the relevant fields for the selected shape


document.getElementById(shape + '-fields').classList.remove('hidden');

// Set the hidden input with the shape type


document.getElementById('shape-type').value = shape;
}
</script>
</body>
</html>

OUTPUT
11

Practical 3
Compute and print roots of quadratic equation.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quadratic Equation Solver</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input[type="number"] {
width: 50px;
padding: 5px;
margin: 10px 5px;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
12

</head>
<body>
<div class="container">
<h2>Quadratic Equation Solver</h2>
<form method="post">
<label for="a">a:</label>
<input type="number" id="a" name="a" step="any" required>
<label for="b">b:</label>
<input type="number" id="b" name="b" step="any" required>
<label for="c">c:</label>
<input type="number" id="c" name="c" step="any" required>
<br>
<input type="submit" value="Find Roots">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$a = $_POST["a"];
$b = $_POST["b"];
$c = $_POST["c"];

function findRoots($a, $b, $c) {


$discriminant = $b * $b - 4 * $a * $c;

if ($discriminant > 0) {
$root1 = (-$b + sqrt($discriminant)) / (2 * $a);
$root2 = (-$b - sqrt($discriminant)) / (2 * $a);
echo "<div class='result'>Roots are real and distinct:<br>";
echo "Root 1: " . $root1 . "<br>";
echo "Root 2: " . $root2 . "</div>";
} elseif ($discriminant == 0) {
$root = -$b / (2 * $a);
echo "<div class='result'>Roots are real and identical:<br>";
echo "Root: " . $root . "</div>";
} else {
$realPart = -$b / (2 * $a);
$imaginaryPart = sqrt(-$discriminant) / (2 * $a);
echo "<div class='result'>Roots are complex and imaginary:<br>";
echo "Root 1: " . $realPart . " + " . $imaginaryPart . "i<br>";
echo "Root 2: " . $realPart . " - " . $imaginaryPart . "i</div>";
}
}

findRoots($a, $b, $c);


}
?>
</div>
</body>
</html>
13

OUTPUT
14

Practical 4
Write a program to determine whether a triangle is isosceles or not?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Isosceles Triangle Checker</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f8ff;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input[type="number"] {
width: 70px;
padding: 5px;
margin: 10px 5px;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
15

</head>
<body>
<div class="container">
<h2>Isosceles Triangle Checker</h2>
<form method="post">
<label for="side1">Side 1:</label>
<input type="number" id="side1" name="side1" step="any" required>
<label for="side2">Side 2:</label>
<input type="number" id="side2" name="side2" step="any" required>
<label for="side3">Side 3:</label>
<input type="number" id="side3" name="side3" step="any" required>
<br>
<input type="submit" value="Check Triangle">
</form>

<?php
OUT
$side1 = $_POST["side1"];
$side2 = $_POST["side2"];
$side3 = $_POST["side3"];

function isIsosceles($side1, $side2, $side3) {


// Check if any two sides are equal
if ($side1 == $side2 || $side1 == $side3 || $side2 == $side3) {
return true;
} else {
return false;
}
}

if (isIsosceles($side1, $side2, $side3)) {


echo "<div class='result'>The triangle is <strong>isosceles</strong>.</div>";
} else {
echo "<div class='result'>The triangle is <strong>not isosceles</strong>.</div>";
}
}
?>
</div>
</body>
</html>

OUTPUT

Practical 5
16

Practical 5
Print multiplication table of a number input by the user.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiplication Table Generator</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input[type="number"] {
width: 100px;
padding: 5px;
margin: 10px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
table {
margin-top: 20px;
border-collapse: collapse;
width: 100%;
}
table, th, td {
border: 1px solid #ddd;
17

}
th, td {
padding: 8px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<div class="container">
<h2>Multiplication Table Generator</h2>
<form method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<br>
<input type="submit" value="Generate Table">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST["number"];

echo "<table>";
echo "<tr><th>Multiplication Table of $number</th></tr>";

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


$result = $number * $i;
echo "<tr><td>$number x $i = $result</td></tr>";
}

echo "</table>";
}
?>
</div>
</body>
</html>
18

OUTPUT
19

Practical 6
Calculate sum of natural numbers from one to n number.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sum of Natural Numbers</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input[type="number"] {
width: 100px;
padding: 5px;
margin: 10px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
20

</head>
<body>
<div class="container">
<h2>Sum of Natural Numbers Calculator</h2>
<form method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" min="1" required>
<br>
<input type="submit" value="Calculate Sum">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n = $_POST["number"];

// Calculate the sum of natural numbers from 1 to n


$sum = ($n * ($n + 1)) / 2;

echo "<div class='result'>The sum of natural numbers from 1 to $n is: $sum</div>";


}
?>
</div>
</body>
</html>

OUTPUT
21

Practical 7
Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8 13 21…..n

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fibonacci Series Generator</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input[type="number"] {
width: 100px;
padding: 5px;
margin: 10px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
</head>
22

<body>
<div class="container">
<h2>Fibonacci Series Generator</h2>
<form method="post">
<label for="number">Enter the number of terms:</label>
<input type="number" id="number" name="number" min="1" required>
<br>
<input type="submit" value="Generate Series">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n = $_POST["number"];

function generateFibonacci($n) {
$fib = [0, 1];
for ($i = 2; $i < $n; $i++) {
$fib[$i] = $fib[$i-1] + $fib[$i-2];
}
return $fib;
}

$fibonacciSeries = generateFibonacci($n);

echo "<div class='result'>Fibonacci series up to $n terms: <br>";


echo implode(" ", $fibonacciSeries);
echo "</div>";
}
?>
</div>
</body>
</html>

OUTPUT
23

Practical 8
Write a program to find the factorial of any number

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Factorial Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input[type="number"] {
width: 100px;
padding: 5px;
margin: 10px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
</head>
24

<body>
<div class="container">
<h2>Factorial Calculator</h2>
<form method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" min="0" required>
<br>
<input type="submit" value="Calculate Factorial">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n = $_POST["number"];

function factorial($n) {
if ($n == 0 || $n == 1) {
return 1;
} else {
return $n * factorial($n - 1);
}
}

$result = factorial($n);

echo "<div class='result'>The factorial of $n is: $result</div>";


}
?>
</div>
</body>
</html>

OUTPUT
25

Practical 9
Determine prime numbers within a specific range.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime Numbers in a Range</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input[type="number"] {
width: 100px;
padding: 5px;
margin: 10px 5px;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
</head>
<body>
26

<div class="container">
<h2>Prime Numbers in a Range</h2>
<form method="post">
<label for="start">Start:</label>
<input type="number" id="start" name="start" min="2" required>
<label for="end">End:</label>
<input type="number" id="end" name="end" required>
<br>
<input type="submit" value="Find Primes">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$start = $_POST["start"];
$end = $_POST["end"];

function isPrime($num) {
if ($num < 2) return false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
return false;
}
}
return true;
}

echo "<div class='result'>Prime numbers between $start and $end:<br>";

$primes = [];
for ($i = $start; $i <= $end; $i++) {
if (isPrime($i)) {
$primes[] = $i;
}
}

echo implode(", ", $primes);


echo "</div>";
}
?>
</div>
</body>
</html>

OUTPUT
27

Practical 10
Write a program to compute, the Average and Grade of students
marks.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Marks Average and Grade</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
width: 300px;
}
input[type="number"] {
width: 70px;
padding: 5px;
margin: 5px 5px;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
28

}
</style>
</head>
<body>
<div class="container">
<h2>Average and Grade Calculator</h2>
<form method="post">
<label for="marks">Enter student marks (comma separated):</label>
<input type="text" id="marks" name="marks" placeholder="e.g., 85, 90, 78" required>
<br>
<input type="submit" value="Calculate">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$marks_input = $_POST["marks"];
$marks = array_map('trim', explode(',', $marks_input));
$total_marks = 0;
$num_of_students = count($marks);

// Validate input
foreach ($marks as $mark) {
if (!is_numeric($mark) || $mark < 0 || $mark > 100) {
echo "<div class='result'>Please enter valid marks between 0 and 100.</div>";
exit;
}
$total_marks += $mark;
}

$average = $total_marks / $num_of_students;

// Determine grade
if ($average >= 90) {
$grade = 'A';
} elseif ($average >= 80) {
$grade = 'B';
} elseif ($average >= 70) {
$grade = 'C';
} elseif ($average >= 60) {
$grade = 'D';
} else {
$grade = 'F';
}

echo "<div class='result'>Average Marks: " . number_format($average, 2) . "<br>";


echo "Grade: " . $grade . "</div>";
}
?>
</div>
</body>
</html>
29

OUTPUT
30

Practical 11
Compute addition, subtraction and multiplication of a matrix.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Matrix Operations</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input[type="number"] {
width: 50px;
padding: 5px;
margin: 5px;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
table {
31

margin-top: 20px;
border-collapse: collapse;
width: 100%;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 8px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<div class="container">
<h2>Matrix Operations</h2>
<form method="post">
<h3>Matrix A</h3>
<label for="a11">A11:</label><input type="number" id="a11" name="a11" step="any"
required>
<label for="a12">A12:</label><input type="number" id="a12" name="a12" step="any"
required><br>
<label for="a21">A21:</label><input type="number" id="a21" name="a21" step="any"
required>
<label for="a22">A22:</label><input type="number" id="a22" name="a22" step="any"
required><br>

<h3>Matrix B</h3>
<label for="b11">B11:</label><input type="number" id="b11" name="b11" step="any"
required>
<label for="b12">B12:</label><input type="number" id="b12" name="b12" step="any"
required><br>
<label for="b21">B21:</label><input type="number" id="b21" name="b21" step="any"
required>
<label for="b22">B22:</label><input type="number" id="b22" name="b22" step="any"
required><br>

<input type="submit" value="Compute Operations">


</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Matrix A
$a11 = $_POST["a11"];
$a12 = $_POST["a12"];
$a21 = $_POST["a21"];
$a22 = $_POST["a22"];

// Matrix B
$b11 = $_POST["b11"];
32

$b12 = $_POST["b12"];
$b21 = $_POST["b21"];
$b22 = $_POST["b22"];

// Addition
$add = [
[$a11 + $b11, $a12 + $b12],
[$a21 + $b21, $a22 + $b22]
];

// Subtraction
$sub = [
[$a11 - $b11, $a12 - $b12],
[$a21 - $b21, $a22 - $b22]
];

// Multiplication
$mul = [
[
$a11 * $b11 + $a12 * $b21,
$a11 * $b12 + $a12 * $b22
],
[
$a21 * $b11 + $a22 * $b21,
$a21 * $b12 + $a22 * $b22
]
];

function displayMatrix($matrix, $title) {


echo "<h3>$title</h3>";
echo "<table>";
foreach ($matrix as $row) {
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . number_format($value, 2) . "</td>";
}
echo "</tr>";
}
echo "</table>";
}

echo "<div class='result'>";


displayMatrix($add, "Matrix Addition");
displayMatrix($sub, "Matrix Subtraction");
displayMatrix($mul, "Matrix Multiplication");
echo "</div>";
}
?>
</div>
</body>
</html>
33

OUTPUT
34

Practical 12
Count total number of vowels in a word “Develop & Empower
Individuals”.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Count Vowels in Phrase</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
width: 300px;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
</head>
<body>
<div class="container">
<h2>Count Vowels in Phrase</h2>

<?php
$phrase = "Develop & Empower Individuals";

// Convert phrase to lowercase to handle case insensitivity


$phrase = strtolower($phrase);

// Define vowels
$vowels = ['a', 'e', 'i', 'o', 'u'];
$vowelCount = 0;

// Count vowels
35

for ($i = 0; $i < strlen($phrase); $i++) {


if (in_array($phrase[$i], $vowels)) {
$vowelCount++;
}
}

echo "<div class='result'>The total number of vowels in \"$phrase\" is: $vowelCount</div>";


?>
</div>
</body>
</html>

OUTPUT
36

Practical 13
Determine whether a string is palindrome or not?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palindrome Checker</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
width: 300px;
}
input[type="text"] {
width: 80%;
padding: 5px;
margin: 10px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
37

</head>
<body>
<div class="container">
<h2>Palindrome Checker</h2>
<form method="post">
<label for="string">Enter a string:</label>
<input type="text" id="string" name="string" required>
<br>
<input type="submit" value="Check Palindrome">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$string = $_POST["string"];

// Function to check if the string is a palindrome


function isPalindrome($str) {
// Remove non-alphanumeric characters and convert to lowercase
$cleanedStr = preg_replace("/[^a-zA-Z0-9]/", "", strtolower($str));
// Check if the cleaned string is equal to its reverse
return $cleanedStr === strrev($cleanedStr);
}

if (isPalindrome($string)) {
echo "<div class='result'>\"$string\" is a palindrome.</div>";
} else {
echo "<div class='result'>\"$string\" is not a palindrome.</div>";
}
}
?>
</div>
</body>
</html>

OUTPUT
38

Practical 14
Display word after Sorting in alphabetical order.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alphabetical Order Sorter</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
width: 300px;
}
input[type="text"] {
width: 80%;
padding: 5px;
margin: 10px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
39

</head>
<body>
<div class="container">
<h2>Alphabetical Order Sorter</h2>
<form method="post">
<label for="word">Enter a word:</label>
<input type="text" id="word" name="word" required>
<br>
<input type="submit" value="Sort Alphabetically">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$word = $_POST["word"];

// Function to sort the characters of the string alphabetically


function sortAlphabetically($str) {
// Convert string to an array of characters
$characters = str_split($str);
// Sort the array
sort($characters);
// Join the array back into a string
return implode('', $characters);
}

$sortedWord = sortAlphabetically($word);

echo "<div class='result'>Original Word: \"$word\"<br>";


echo "Sorted Word: \"$sortedWord\"</div>";
}
?>
</div>
</body>
</html>

OUTPUT
40

Practical 15
Check whether a number is in a given range using functions.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Range Checker</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
width: 300px;
}
input[type="number"] {
width: 70px;
padding: 5px;
margin: 5px;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
41

</head>
<body>
<div class="container">
<h2>Check Number in Range</h2>
<form method="post">
<label for="number">Number:</label>
<input type="number" id="number" name="number" required>
<br>
<label for="min">Min Range:</label>
<input type="number" id="min" name="min" required>
<br>
<label for="max">Max Range:</label>
<input type="number" id="max" name="max" required>
<br>
<input type="submit" value="Check Range">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST["number"];
$min = $_POST["min"];
$max = $_POST["max"];

// Function to check if a number is within a given range


function isInRange($num, $minRange, $maxRange) {
return $num >= $minRange && $num <= $maxRange;
}

if (isInRange($number, $min, $max)) {


echo "<div class='result'>$number is within the range [$min, $max].</div>";
} else {
echo "<div class='result'>$number is not within the range [$min, $max].</div>";
}
}
?>
</div>
</body>
</html>

OUTPUT
42

Practical 16
Write a program accepts a string and calculates number of upper case
letters and lower case letters available in that string.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Count Uppercase and Lowercase Letters</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
width: 300px;
}
input[type="text"] {
width: 80%;
padding: 5px;
margin: 10px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
43

</style>
</head>
<body>
<div class="container">
<h2>Count Uppercase and Lowercase Letters</h2>
<form method="post">
<label for="string">Enter a string:</label>
<input type="text" id="string" name="string" required>
<br>
<input type="submit" value="Count Letters">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$string = $_POST["string"];

// Function to count uppercase and lowercase letters


function countCaseLetters($str) {
$upperCount = 0;
$lowerCount = 0;

// Iterate through each character in the string


for ($i = 0; $i < strlen($str); $i++) {
if (ctype_upper($str[$i])) {
$upperCount++;
} elseif (ctype_lower($str[$i])) {
$lowerCount++;
}
}

return [$upperCount, $lowerCount];


}

list($upperCount, $lowerCount) = countCaseLetters($string);

echo "<div class='result'>The string \"$string\" contains:<br>";


echo "Uppercase letters: $upperCount<br>";
echo "Lowercase letters: $lowerCount</div>";
}
?>
</div>
</body>
</html>

OUTPUT
44

Practical 17
Design a program to reverse a string word by word.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reverse String Word by Word</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
width: 400px;
}
input[type="text"] {
width: 80%;
padding: 5px;
margin: 10px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.result {
margin-top: 20px;
font-size: 18px;
color: #333;
}
</style>
45

</head>
<body>
<div class="container">
<h2>Reverse String Word by Word</h2>
<form method="post">
<label for="string">Enter a string:</label>
<input type="text" id="string" name="string" required>
<br>
<input type="submit" value="Reverse Words">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$string = $_POST["string"];

// Function to reverse each word in the string


function reverseWords($str) {
// Split the string into words
$words = explode(' ', $str);
// Reverse each word
$reversedWords = array_map('strrev', $words);
// Join the words back into a string
return implode(' ', $reversedWords);
}

$reversedString = reverseWords($string);

echo "<div class='result'>Original String: \"$string\"<br>";


echo "Reversed Words: \"$reversedString\"</div>";
}
?>
</div>
</body>
</html>

OUTPUT
46

Practical 18
Write a program to create a login form. On submitting the form,
the user should navigate to profile page.

<?php
session_start();

// Handle logout
if (isset($_GET['logout'])) {
session_destroy();
header("Location: " . $_SERVER['PHP_SELF']);
exit();
}

// Handle login
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];

// Simple validation (replace with real authentication in production)


if ($username === "user" && $password === "password") {
$_SESSION['loggedin'] = true;
header("Location: " . $_SERVER['PHP_SELF']);
exit();
} else {
$error = "Invalid username or password.";
}
}

// Check if user is logged in


$isLoggedIn = isset($_SESSION['loggedin']) && $_SESSION['loggedin'];
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login and Profile</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #ffffff;
47

padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
width: 300px;
}
input[type="text"], input[type="password"] {
width: 80%;
padding: 10px;
margin: 10px 0;
border-radius: 4px;
border: 1px solid #ccc;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.error {
color: red;
}
a{
color: #4CAF50;
text-decoration: none;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<?php if (!$isLoggedIn): ?>
<h2>Login</h2>
<form method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br>
<input type="submit" value="Login">
</form>
<?php if (isset($error)): ?>
<div class='error'><?php echo $error; ?></div>
<?php endif; ?>
<?php else: ?>
<h2>Welcome to Your Profile</h2>
<p>You have successfully logged in.</p>
48

<a href="?logout">Logout</a>
<?php endif; ?>
</div>
</body>
</html>

OUTPUT
49

Program 19
Design front page of a college or department using graphics
method.

<?php
// Define variables for dynamic content
$logoSrc =
"https://static.educativ.net/monthly_2020_10/12832397_1015263555207557_209872829008510722
4_n.thumb.jpg.7e6a6ca79d1fbb5c6c2a9d5f082efba7.jpg";
$heroImage =
"https://content.jdmagicbox.com/v2/comp/pathankot/m3/9999px186.x186.140703151605.m1m3/ca
talogue/a-and-m-institute-of-management-and-technology-pathankot-pathankot-colleges-
v70ck1n5mu.jpg";
$collegeName = "A & M Institute of Management and Technology";
$currentYear = date("Y");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $collegeName; ?> Front Page</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
header {
background: #007bff;
color: white;
padding: 20px;
text-align: center;
border-bottom: 5px solid #0056b3;
}
header .logo img {
max-width: 150px;
}
nav {
background: #343a40;
padding: 10px 0;
border-bottom: 2px solid #495057;
}
nav ul {
list-style-type: none;
padding: 0;
margin: 0;
display: flex;
justify-content: center;
50

}
nav ul li {
margin: 0;
}
nav ul li a {
color: white;
padding: 10px 20px;
text-decoration: none;
font-weight: bold;
display: block;
}
nav ul li a:hover {
background: #495057;
}
.hero {
background: url("<?php echo $heroImage; ?>") no-repeat center center;
background-size: cover;
height: 250px;
display: flex;
align-items: center;
justify-content: center;
color: white;
text-align: center;
margin-bottom: 20px;
border-bottom: 3px solid #007bff;
}
.hero h1 {
font-size: 2em;
margin: 0;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.6);
}
.container {
max-width: 900px;
margin: 20px auto;
padding: 20px;
background: white;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border: 1px solid #ddd;
}
.section {
margin-bottom: 20px;
}
.section h2 {
color: #007bff;
border-bottom: 2px solid #007bff;
padding-bottom: 10px;
margin-bottom: 15px;
}
footer {
background: #343a40;
color: white;
text-align: center;
51

padding: 10px;
border-top: 2px solid #495057;
margin-top: 20px;
}
.contact-form {
display: flex;
flex-direction: column;
max-width: 600px;
margin: 0 auto;
}
.contact-form input, .contact-form textarea {
padding: 10px;
margin: 10px 0;
border: 1px solid #ced4da;
border-radius: 4px;
}
.contact-form button {
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.contact-form button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<header>
<div class="logo">
<img src="<?php echo $logoSrc; ?>" alt="College Logo">
</div>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#departments">Departments</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>

<div class="hero">
<h1>Welcome to <?php echo $collegeName; ?></h1>
</div>

<div class="container">
<section id="about" class="section">
<h2>About Us</h2>
52

<p>Education embraces not only facts but also values—it inculcates such good
practices which may help reveal our qualities and perfections in every sphere of life. <?php echo
$collegeName; ?> endeavors to impart such value-based education to our students. Established in
2008, this institute was created by the elites of Pathankot to provide Management and Technical
Education. It blends culture, tradition, and modernity to shape and mold students' personalities in a
healthy way. Today, <?php echo $collegeName; ?> has grown in status and strength, becoming one of
the finest educational programs, equipping students with qualities to confidently face emerging
changes and achieve their goals with focused vision.</p>
</section>

<section id="departments" class="section">


<h2>Departments</h2>
<p>Explore our various departments and discover the wide array of programs we offer.
From Science to Humanities, each department provides specialized knowledge and hands-on
experience.</p>
</section>

<section id="contact" class="section">


<h2>Contact Us</h2>
<p>Have questions or need more information? Reach out to us through the form
below or contact our office directly. We are here to assist you with any inquiries you may have.</p>
<form class="contact-form" method="post" action="#">
<input type="text" name="name" placeholder="Your Name" required>
<input type="email" name="email" placeholder="Your Email" required>
<textarea name="message" rows="4" placeholder="Your Message"
required></textarea>
<button type="submit">Send Message</button>
</form>
</section>
</div>

<footer>
<p>&copy; <?php echo $currentYear; ?> <?php echo $collegeName; ?>. All rights
reserved.</p>
</footer>
</body>
</html>
53

OUTPUT
54

Practical 20
Write a program to upload and download files.

<?php
// Directory where files will be uploaded
$uploadDir = 'uploads/';

// Check if the upload directory exists, if not create it


if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0777, true);
}

// Handle file upload


if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$fileName = basename($_FILES['file']['name']);
$targetFilePath = $uploadDir . $fileName;

// Check if the file was uploaded without errors


if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
// Move the uploaded file to the target directory
if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFilePath)) {
$uploadMessage = 'File uploaded successfully.';
} else {
$uploadMessage = 'Failed to upload file.';
}
} else {
$uploadMessage = 'Error uploading file.';
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload and Download</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background: white;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
55

}
h1 {
text-align: center;
color: #007bff;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input[type="file"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-group input[type="submit"] {
background-color: #007bff;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
}
.form-group input[type="submit"]:hover {
background-color: #0056b3;
}
.file-list {
margin-top: 20px;
}
.file-list a {
display: block;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 10px;
text-decoration: none;
color: #007bff;
}
.file-list a:hover {
background-color: #f4f4f4;
}
.message {
margin-bottom: 20px;
color: #d9534f;
}
</style>
</head>
<body>
<div class="container">
<h1>Upload and Download Files</h1>
56

<!-- Display upload message -->


<?php if (isset($uploadMessage)): ?>
<div class="message"><?php echo $uploadMessage; ?></div>
<?php endif; ?>

<!-- File Upload Form -->


<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="file">Select file to upload:</label>
<input type="file" name="file" id="file" required>
</div>
<div class="form-group">
<input type="submit" value="Upload File">
</div>
</form>

<!-- List of Uploaded Files -->


<div class="file-list">
<h2>Uploaded Files</h2>
<?php
if (is_dir($uploadDir)) {
$files = scandir($uploadDir);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
echo '<a href="' . $uploadDir . $file . '" download>' . $file . '</a>';
}
}
} else {
echo '<p>No files found.</p>';
}
?>
</div>
</div>
</body>
</html>

OUTPUT

You might also like