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

ET22BTEC010 - Diya Patel - Practical 9 - PHP Basics

Uploaded by

0611diyapatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

ET22BTEC010 - Diya Patel - Practical 9 - PHP Basics

Uploaded by

0611diyapatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Subject Code:-BTCO15504

Subject Name:- Web Technology Concepts


Name :- Diya Patel
Enrollment Number:- ET22BTEC010

Practical 9: PHP Basics


1) Write PHP Script to print Fibonacci series.
Program:-
<?php
// Number of terms
$n = 10;
// First two terms
$first = 0;
$second = 1;
echo "Fibonacci Series:\n";
// Print the first two terms
echo $first . ", " . $second . ", ";
// Loop to generate the remaining terms
for ($i = 2; $i < $n; $i++) {
$next = $first + $second;
echo $next;
if ($i < $n - 1) {
echo ", ";
}
$first = $second;
$second = $next;
}
echo "\n";
?>

Output:-

2) Write PHP Script to find maximum number out of three given numbers.
Program:-
<?php
// Function to find the maximum of three numbers
function findMax($a, $b, $c) {
if ($a >= $b && $a >= $c) {
return $a;
} elseif ($b >= $a && $b >= $c) {
return $b;
} else {

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 106


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

return $c;
}
}
// Example numbers
$num1 = 10;
$num2 = 15;
$num3 = 5;
// Call the function and display the result
$max = findMax($num1, $num2, $num3);
echo "The maximum number is: " . $max;
?>

Output:-

3) Write a PHP program to compute the sum of the prime numbers less than 100.
Program:-
<?php
function isPrime($num) {
if ($num < 2) return false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) return false;
}
return true;
}
$sum = 0;
for ($i = 2; $i < 100; $i++) {
if (isPrime($i)) {
$sum += $i;
}
}
echo "The sum of prime numbers less than 100 is: " . $sum;
?>

Output:-

4) Find the Transpose of a matrix, Multiplication of two matrices and Addition of two matrices.
Program:-
<?php

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 107


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

// Function to transpose a matrix


function transpose($matrix) {
$transposed = array();
foreach ($matrix as $row => $columns) {
foreach ($columns as $column => $value) {
$transposed[$column][$row] = $value;
}
}
return $transposed;
}

// Function to multiply two matrices


function multiplyMatrices($matrix1, $matrix2) {
$result = array();
$rowCount = count($matrix1);
$colCount = count($matrix2[0]);
$n = count($matrix2);
for ($i = 0; $i < $rowCount; $i++) {
for ($j = 0; $j < $colCount; $j++) {
$result[$i][$j] = 0;
for ($k = 0; $k < $n; $k++) {
$result[$i][$j] += $matrix1[$i][$k] * $matrix2[$k][$j];
}
}
}
return $result;
}
// Function to add two matrices
function addMatrices($matrix1, $matrix2) {
$result = array();
$rowCount = count($matrix1);
$colCount = count($matrix1[0]);
for ($i = 0; $i < $rowCount; $i++) {
for ($j = 0; $j < $colCount; $j++) {
$result[$i][$j] = $matrix1[$i][$j] + $matrix2[$i][$j];
}
}
return $result;
}
// Example matrices
$matrix1 = [
[1, 2, 3],
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 108
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

[4, 5, 6]
];
$matrix2 = [
[7, 8],
[9, 10],

[11, 12]
];
// Compute transpose of matrix1
$transpose1 = transpose($matrix1);
// Compute multiplication of matrix1 and matrix2
$multiplication = multiplyMatrices($matrix1, $matrix2);
// Example matrices for addition (must be the same size)
$matrix3 = [
[1, 2, 3],
[4, 5, 6]
];
$matrix4 = [
[7, 8, 9],
[10, 11, 12]
];
// Compute addition of matrix3 and matrix4
$addition = addMatrices($matrix3, $matrix4);
echo "Transpose of matrix1:\n";
print_r($transpose1);
echo "\nMultiplication of matrix1 and matrix2:\n";
print_r($multiplication);
echo "\nAddition of matrix3 and matrix4:\n";
print_r($addition);
?>

Output:-

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 109


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 110


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

5) Write PHP script Using user defined function.


Program:-
<?php
// User-defined function to find the maximum of three numbers

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 111


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

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


if ($a >= $b && $a >= $c) {
return $a;
} elseif ($b >= $a && $b >= $c) {
return $b;
} else {
return $c;
}
}
// Example numbers
$num1 = 12;
$num2 = 24;
$num3 = 18;
// Call the function and display the result
$max = findMax($num1, $num2, $num3);
echo "The maximum number among $num1, $num2, and $num3 is: " . $max;
?>

Output:-

6) Create form using text box, check box, radio button, select, submit button. And display user
inserted value in new PHP page
Program:-
HTML CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Form</title>
</head>
<body>
<form action="test.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="gender">Gender:</label>
<input type="radio" id="male" name="gender" value="Male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="Female">

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 112


Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

<label for="female">Female</label><br><br>
<label for="hobbies">Hobbies:</label>
<input type="checkbox" id="reading" name="hobbies[]" value="Reading">
<label for="reading">Reading</label>
<input type="checkbox" id="traveling" name="hobbies[]" value="Traveling">
<label for="traveling">Traveling</label>
<input type="checkbox" id="sports" name="hobbies[]" value="Sports">
<label for="sports">Sports</label><br><br>
<label for="country">Country:</label>
<select id="country" name="country">
<option value="India">India</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

PHP CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Submission</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
$gender = isset($_POST['gender']) ? htmlspecialchars($_POST['gender']) : 'Not specified';
$hobbies = isset($_POST['hobbies']) ? $_POST['hobbies'] : array();
$country = htmlspecialchars($_POST['country']);
echo "<h1>Submitted Information</h1>";
echo "Name: " . $name . "<br>";
echo "Gender: " . $gender . "<br>";
echo "Hobbies: " . implode(", ", $hobbies) . "<br>";
echo "Country: " . $country . "<br>";
} else {
echo "Form not submitted correctly.";
}
SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 113
Subject Code:-BTCO15504
Subject Name:- Web Technology Concepts
Name :- Diya Patel
Enrollment Number:- ET22BTEC010

?>
</body>
</html>

Output:-

SCET/EC/2024-25/ODD/BTech/Sem-V Pg no:- 114

You might also like