PHP Practical File
PHP Practical File
Technology,Pathankot
Practical File
Programming in PHP
UGCA 1930
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
<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;
}
}
?>
<script>
function showForm(shape) {
// Hide all shape fields initially
document.querySelectorAll('.shape-fields').forEach(function(field) {
field.classList.add('hidden');
});
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"];
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>";
}
}
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"];
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>";
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"];
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);
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);
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;
}
$primes = [];
for ($i = $start; $i <= $end; $i++) {
if (isPrime($i)) {
$primes[] = $i;
}
}
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;
}
// 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';
}
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>
<?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
]
];
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";
// Define vowels
$vowels = ['a', 'e', 'i', 'o', 'u'];
$vowelCount = 0;
// Count vowels
35
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"];
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"];
$sortedWord = sortAlphabetically($word);
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"];
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"];
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"];
$reversedString = reverseWords($string);
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"];
<!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>
<footer>
<p>© <?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/';
<!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
OUTPUT