ET22BTEC010 - Diya Patel - Practical 9 - PHP Basics
ET22BTEC010 - Diya Patel - Practical 9 - PHP Basics
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 {
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
[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:-
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">
<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:-