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

PHP Pratical File

This document is a practical file for a PHP programming course, detailing various programming exercises and their corresponding code implementations. It includes tasks such as computing arithmetic operations, calculating areas of shapes, solving quadratic equations, and working with strings and matrices. The file is submitted by a student named Aryan and covers a range of programming concepts suitable for a 5th-semester curriculum.

Uploaded by

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

PHP Pratical File

This document is a practical file for a PHP programming course, detailing various programming exercises and their corresponding code implementations. It includes tasks such as computing arithmetic operations, calculating areas of shapes, solving quadratic equations, and working with strings and matrices. The file is submitted by a student named Aryan and covers a range of programming concepts suitable for a 5th-semester curriculum.

Uploaded by

Aryan -
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

PCTE Group of

Institutes

Semester: -5th
Programming in PHP Laboratory
UGCA1930

Practical file

Name: Aryan
Class: BCA-3’C’
Roll_No:2223152
1
Submitted To: Miss Punita Mam

Page .n
Sr.no Title o Sign's
Take values from the user and compute sum,
subtraction, multiplication, division and exponent of
1 value of the variables. 3
Write a program to find area of following shapes:
circle, rectangle, triangle, square, trapezoid and
2 parallelogram. 4-6
3 Compute and print roots of quadratic equation. 6,7
Write a program to determine whether a triangle is
4 isosceles or not? 8
Print multiplication table of a number input by the
5 user. 9
Calculate sum of natural numbers from one to n
6 number. 10
Print Fibonacci series up to n numbers e.g. 0 1 1 2 3
7 5 8 13 21…..n 11
Write a program to find the factorial of any number.
8 12
9 Determine prime numbers within a specific range. 13,14
Write a program to compute, the Average and
10 Grade of student’s marks. 15,16
Compute addition, subtraction and multiplication of
11 a matrix. 17-20
Count total number of vowels in a word “Develop &
12 Empower Individuals”. 21
13 Determine whether a string is palindrome or not? 22
14 Display word after Sorting in alphabetical order. 23
Check whether a number is in a given range using
15 functions. 24,25
Write a program accepts a string and calculates
number of upper-case letters and lower-case letters
16 available in that string. 26,27
Design a program to reverse a string word by word.
17 28
Write a program to create a login form. On
submitting the form, the user should navigate to
18 profile page. 29-34
Design front page of a college or department using
19 graphics method. 35-38
20 Write a program to upload and download files. 39-42
2
Pratical-1

Take values from the user and compute sum, subtraction,


multiplication, division and exponent of value of the
variables.

<?php

$x=100;

$y=60;

echo "The sum of x and y is : ". ($x+$y)."\n";

echo "The difference between x and y is : ". ($x-$y) ."\n";

echo "Multiplication of x and y : ". ($x*$y) ."\n";

echo "Division of x and y : ". ($x/$y) ."\n";

echo "Modulus of x and y : " . ($x%$y) ."\n";

?>

3
Pratical-2

Write a program to find area of following shapes: circle,


rectangle, triangle, square, trapezoid and parallelogram.

 Circle
 Rectangle
 Triangle
 Square
 Trapezoid
 Parallelogram

<?php

function areaOfCircle($radius) {

return pi() * pow($radius, 2);

function areaOfRectangle($length, $width) {

return $length * $width;

function areaOfTriangle($base, $height) {

return 0.5 * $base * $height;

function areaOfSquare($side) {

4
return pow($side, 2);

function areaOfTrapezoid($base1, $base2, $height) {

return 0.5 * ($base1 + $base2) * $height;

function areaOfParallelogram($base, $height) {

return $base * $height;

echo "Area of Circle: " . areaOfCircle(10) . " \n";

echo "Area of Rectangle: " . areaOfRectangle(10, 20) . " \n";

echo "Area of Triangle: " . areaOfTriangle(10, 20) . "\n";

echo "Area of Square: " . areaOfSquare(10) . " \n";

echo "Area of Trapezoid: " . areaOfTrapezoid(10, 20, 30) . "\n";

echo "Area of Parallelogram: " . areaOfParallelogram(10, 20) . " \n";

?>

5
Pratical-3

Compute and print roots of quadratic equation.

<?php

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

$d = $b**2 - 4*$a*$c;

if ($d > 0) {

$root1 = (-$b + sqrt($d)) / (2*$a);

$root2 = (-$b - sqrt($d)) / (2*$a);

return [$root1, $root2];

} else if ($d == 0) {

$root = -$b / (2*$a);

return [$root];

} else {

return [];

6
}

$a = -19;

$b = 29;

$c = 50;

$roots = solveQuadratic($a, $b, $c);

echo "Roots: " . implode(', ', $roots);

?>

7
Pratical-4

Write a Program to find whether a Triangle is Isosceles or


Not.

<?php

$side1 = 5;

$side2 = 5;

$side3 = 3;

if ($side1 == $side2 || $side1 == $side3 || $side2 == $side3) {

echo"The triangle is isosceles.";

} else {

echo"The triangle is not isosceles.";

?>

8
Pratical-5

Print multiplication table of a number input by the user.

<?php

$table = 10;

$length = 10;

$i = 1;

echo "Multiplication table: $table \n";

for($i=1; $i<=$length; $i++)

echo " $table* $i = ". $table* $i. "\n";

?>

9
Pratical-6

Calculate Sum of Natural Numbers from One to n Numbers.

<?php

$N = 5;

$sum = 0;

for ($i = 1; $i <= $N; $i++)

$sum = $sum + $i;

echo "Sum of first " . $N .

" Natural Numbers : " . $sum;

?>

10
Pratical-7
Print the Fibonacci Series from up to n numbers e.g. 0 1 1 2
3 5 8………n.

<?php

function fibonacciSeries($n){

$num1 = 0;

$num2 = 1;

echo"Fibonacci Series for First 10 Numbers:"."\n";

for ( $i = 0; $i < $n; $i++ ) {

echo $num1 . ", ";

$num3 = $num1 + $num2;

$num1 = $num2;

$num2 = $num3;

$n = 10;

fibonacciSeries($n);

?>

11
Pratical-8

Write a Program to Print Factorial of any Number.

<?php

$n = 5;

$f = 1;

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

$f = $f * $i;

echo "Factorial of $n is $f";

?>

12
Pratical-9

Determine Prime Numbers within a specific range.

<?php

$count = 0;

$num = 2;

while ($count < 15 )

$div_count=0;

for ( $i=1; $i<=$num; $i++)

if (($num%$i)==0)

$div_count++;

if ($div_count<3)

echo $num." , ";

13
$count=$count+1;

$num=$num+1;

?>

14
Pratical-10

Compute the average & Grade of Students Marks.

<?php

function calculateMarks($marks) {

$total = array_sum($marks);

$average = $total / count($marks);

if ($average >= 60) {

$grade = "A";

} elseif ($average >= 50) {

$grade = "B";

} elseif ($average >= 40) {

$grade = "C";

} elseif ($average >= 30) {

$grade = "D";

} else {

$grade = "F";

return [

15
'total' => $total,

average' => number_format($average, 2),

'grade' => $grade

];

$marks = [55, 48, 60, 56, 44];

$results = calculateMarks($marks);

echo "Total Marks: " . $results['total'] ."\n";

echo "Average Marks: " . $results['average'] ."\n";

echo "Grade: " . $results['grade'] ."\n";

?>

16
Pratical-11

Compute Addition, Subtraction and Multiplication of a


Matrix.

 Addition
<?php

function addMatrices($a, $b) {


$result = [];
for ($i = 0; $i < count($a); $i++) {
for ($j = 0; $j < count($a[$i]); $j++) {
$result[$i][$j] = $a[$i][$j] + $b[$i][$j];
}
}
return $result;
}

$matrixA = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
$matrixB = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
];

$additionResult = addMatrices($matrixA, $matrixB);

function printMatrix($matrix) {
foreach ($matrix as $row) {
echo implode(" ", $row) . "\n";
}
}

17
echo "Addition Result:"."\n";
printMatrix($additionResult);

?>

 Subtraction
<?php

function subtractMatrices($a, $b) {


$result = [];
for ($i = 0; $i < count($a); $i++) {
for ($j = 0; $j < count($a[$i]); $j++) {
$result[$i][$j] = $a[$i][$j] - $b[$i][$j];
}
}
return $result;
}

$matrixA = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
$matrixB = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
];

18
$subtractionResult = subtractMatrices($matrixA, $matrixB);

function printMatrix($matrix) {
foreach ($matrix as $row) {
echo implode(" ", $row) . "\n";
}
}

echo "Subtraction Result:"."\n";


printMatrix($subtractionResult);

?>

 Multiplication
<?php

function MultilpyMatrices($a, $b) {


$result = [];
for ($i = 0; $i < count($a); $i++) {
for ($j = 0; $j < count($a[$i]); $j++) {
$result[$i][$j] = $a[$i][$j] * $b[$i][$j];
}
}
return $result;
}

$matrixA = [
[1, 2, 3],
[4, 5, 6],

19
[7, 8, 9]
];
$matrixB = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
];

$multiplicationResult = MultilpyMatrices($matrixA, $matrixB);

function printMatrix($matrix) {
foreach ($matrix as $row) {
echo implode(" ", $row) . "\n";
}
}

echo "Multiplication Result:"."\n";


printMatrix($multiplicationResult);

?>

20
Pratical-12

Count the total number of vowels in a word


“Develop & Empower Individuals”.

<?php

function countVowels($str)
{
$str = strtolower($str);
$vowelCount = 0;

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


if (in_array($str[$i], ["a", "e", "i", "o", "u"])) {
$vowelCount++;
}
}

return $vowelCount;
}

$str = "Develop & Empower Individuals";


$vowelsCount = countVowels($str);

echo "Number of vowels: " . $vowelsCount;

?>

21
Pratical-13

Determine whether a String is Palindrome or not.

<?php

function isPalindrome($string) {

$cleanedString = strtolower(preg_replace("/[^A-Za-z0-9]/", '', $string));

$reversedString = strrev($cleanedString);

return $cleanedString === $reversedString;

$testString = "A man a plan a canal Panama";

if (isPalindrome($testString)) {

echo "The string \"$testString\" is a palindrome.";

} else {

echo "The string \"$testString\" is not a palindrome.";

?>

22
Pratical-14

Display Word after sorting in alphabetic order.

<?php

function sortWords($inputString) {

$wordsArray = preg_split('/\s+/', trim($inputString));

usort($wordsArray, function($a, $b) {

return strcasecmp($a, $b);

});

return implode(' ', $wordsArray);

$inputString = "Pencil Notebook Book Table PC";

$sortedWords = sortWords($inputString);

echo "Sorted words: " . $sortedWords;

?>

23
Pratical-15

Check whether a number is in given range using


functions.

<?php

function isNumberInRange($number, $min, $max) {

if ($number >= $min && $number <= $max) {

return true;

} else {

return false;

$number = 500;

$minRange = 100;

$maxRange = 1000;

if (isNumberInRange($number, $minRange, $maxRange)) {

echo "$number is in the range of $minRange to $maxRange.";

} else {

echo "$number is not in the range of $minRange to $maxRange.";

24
}

?>

25
Pratical-16

Write a Program accepts a string & calculates number of


upper-case and lower-case letters available in that string.

<?php

function countCaseLetters($inputString) {

$upperCaseCount = 0;

$lowerCaseCount = 0;

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

if (ctype_upper($inputString[$i])) {

$upperCaseCount++;

elseif (ctype_lower($inputString[$i])) {

$lowerCaseCount++;

return [

'uppercase' => $upperCaseCount,

'lowercase' => $lowerCaseCount,

26
];

$inputString = "PCTE Group Of Institues is a having four blocks.";

$result = countCaseLetters($inputString);

echo "Uppercase Letters: " . $result['uppercase'] . "\n";

echo "Lowercase Letters: " . $result['lowercase'] . "\n";

?>

27
Practical-17

Design a program to reverse a String Word by Word.

<?php

function reverseStringWordByWord($inputString) {

$wordsArray = explode(' ', $inputString);

$reversedArray = array_reverse($wordsArray);

$reversedString = implode(' ', $reversedArray);

return $reversedString;

$inputString = "PHP is a Programming Language";

$reversedString = reverseStringWordByWord($inputString);

echo "Original String: $inputString\n";

echo "Reversed String: $reversedString\n";

?>

28
Practial-18

Write a program to create a login form. On submitting


the form, the user should navigate to profile page.

 Username: admin
 Password: password

29
 Welcome
<?php

session_start();

if (!isset($_SESSION['username'])) {

30
header("Location: login.php");
exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Profile Page</title>
</head>
<body>

<h2>Welcome, <?php echo $_SESSION['username']; ?>!</h2>


<p>You have successfully logged in.</p>
<a href="logout.php">Logout</a>

</body>
</html>

 Profile
<?php
session_start();

$valid_username = "admin";
$valid_password = "password";

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

if ($username === $valid_username && $password === $valid_password)


{

$_SESSION['username'] = $username;
header("Location: welcome.php");

31
exit();
} else {
echo "<script>alert('Invalid username or password.');</script>";
echo "<script>window.location.href='login.php';</script>";
}
} else {
header("Location: login.php");
exit();
}
?>

 Login
<?php
session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Login Form</title>
<style>
body {
font-family: Arial, sans-serif;
}
.container {
max-width: 300px;
margin: auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
.container input {
width: 100%;
padding: 10px;
margin: 10px 0;
}

32
.container button {
padding: 10px;
background-color: #5cb85c;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.container button:hover {
background-color: #4cae4c;
}
</style>
</head>
<body>

<div class="container">
<h2>Login</h2>
<form action="profile.php" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>

<label for="password">Password:</label>
<input type="password" id="password" name="password" required>

<button type="submit">Login</button>
</form>
</div>

</body>
</html>

 Logout
<?php
session_start();

$_SESSION = array();

session_destroy();

33
header("Location: login.php");
exit();
?>

34
Practical-19

Design the Front Page of a College or Department using a


graphics method.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-


scale=1.0">

<title>PCTE Group Of Institues-Department of Engineering</title>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<header>

<h1>Welcome to The PCTE Group Of institues</h1>

<h2>Department of Engineering</h2>

<nav>

<ul>

<li><a href="#about">About Us</a></li>


35
<li><a href="#courses">Courses</a></li>

<li><a href="#faculty">Faculty</a></li>

<li><a href="#contact">Contact</a></li>

</ul>

</nav>

</header>

<section id="about">

<h3>About Us</h3>

<p>Welcome to the Department of at PCTE Group Of Institues. We


offer a range of programs designed to help students excel in their
careers.</p>

</section>

<section id="courses">

<h3>Courses Offered</h3>

<ul>

<li>BCA</li>

<li>BTech</li>

<li>MCA</li>

<li>MTech</li>

36
</ul>

</section>

<section id="faculty">

<h3>Meet Our Faculty</h3>

<ul>

<li>Professor A</li>

<li>Professor B</li>

<li>Professor C</li>

</ul>

</section>

<section id="contact">

<h3>Contact Us</h3>

<p>Email: [email protected]</p>

<p>Phone: (123) 456-7890</p>

</section>

<footer>

<p>&copy; <?php echo date("Y"); ?> PCTE Group Of Institues All rights
reserved.</p>

</footer>

37
</body>

</html>

38
Practical-20

Write a Program to upload and download files.

<!DOCTYPE html>

<?php

$target_dir = $_POST["dirname"]."/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = strtolower(pathinfo($target_file,
PATHINFO_EXTENSION));

$extensions = array("jpeg","jpg","png","pdf","gif");

if(isset($_POST["submit"])) {

if(!empty($_POST["dirname"])){

if(!is_dir($_POST["dirname"])) {

mkdir($_POST["dirname"]);

$uploadOk = 1;

39
else {

echo "Specify the directory name...";

$uploadOk = 0;

exit;

if(in_array($imageFileType, $extensions) === true) {

$uploadOk = 1;

else {

echo "No file selected or Invalid file extension...";

$uploadOk = 0;

exit;

if (file_exists($target_file)) {

echo "Sorry, file already exists.";

$uploadOk = 0;

exit;

40
}

if ($_FILES["fileToUpload"]["size"] > 10000000) {

echo "Sorry, your file is too large.";

$uploadOk = 0;

exit;

if ($uploadOk == 0)

echo "Sorry, your file was not uploaded.";

else

if (move_uploaded_file($_FILES["fileToUpload"]

["tmp_name"], $target_file))

echo "The file ". $_FILES["fileToUpload"]

["name"]. " has been uploaded.";

41
else

echo "Sorry, there was an error uploading your file.";

?>

</body>

</html>

42

You might also like