php lab part b manual
php lab part b manual
Sl Part B
No
01 Write a PHP script to implement form handling using get method.
03 Write a PHP script that receive form input by the method post to
check the number is palindrome or not.
04 Write a PHP script that receive string as a form input.
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime Number Checker</title>
</head>
<body>
<h1>Prime Number Checker</h1>
<form action="prime_checker.php" method="POST">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<input type="submit" value="Check">
</form>
<?php
if (isset($is_prime)) {
if ($is_prime) {
echo "<h2>$number is a Prime Number</h2>";
} else {
echo "<h2>$number is Not a Prime Number</h2>";
}
}
?>
</body>
</html>
04.Write a PHP script that receive string as a form input.
<?php
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['input_string'])) {
$input_string = htmlspecialchars($_POST['input_string']); // Sanitize the input to prevent
XSS attacks
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Input Form</title>
</head>
<body>
<h1>Submit a String</h1>
<!-- Form to receive string input -->
<form action="receive_string.php" method="POST">
<label for="input_string">Enter a string:</label>
<input type="text" id="input_string" name="input_string" required>
<input type="submit" value="Submit">
</form>
<?php
if (isset($input_string)) {
echo "<h2>You entered: $input_string</h2>";
}
?>
</body>
</html>
05.Write a PHP script to Compute addition of two matrices as a form input.
<?php
$matrix1 = $matrix2 = $result = [];
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$matrix1 = [
[$_POST['m11'], $_POST['m12']],
[$_POST['m21'], $_POST['m22']]
];
$matrix2 = [
[$_POST['m31'], $_POST['m32']],
[$_POST['m41'], $_POST['m42']]
];
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
$result[$i][$j] = $matrix1[$i][$j] + $matrix2[$i][$j];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Matrix Addition</title>
</head>
<body>
<h1>Matrix Addition</h1>
<!-- Form to receive matrix input -->
<form action="matrix_addition.php" method="POST">
<h2>Enter Matrix 1</h2>
<input type="number" name="m11" required> <input type="number" name="m12"
required><br>
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
<input type="number" name="m21" required> <input type="number" name="m22"
required><br>
<h2>Enter Matrix 2</h2>
<input type="number" name="m31" required> <input type="number" name="m32"
required><br>
<input type="number" name="m41" required> <input type="number" name="m42"
required><br>
<input type="submit" value="Add Matrices">
</form>
<?php if ($_SERVER["REQUEST_METHOD"] == "POST"): ?>
<h2>Result of Matrix Addition:</h2>
<table border="1">
<?php for ($i = 0; $i < 2; $i++): ?>
<tr>
<?php for ($j = 0; $j < 2; $j++): ?>
<td><?= $result[$i][$j] ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
<?php endif; ?>
</body>
</html>
06.Write a PHP script to Show the functionality of date and time function.
<?php
date_default_timezone_set('Asia/Kolkata');
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentTimestamp = time();
$nextDay = date("Y-m-d", strtotime("+1 day")); // Add 1 day
$dayOfWeek = date("l");
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
$monthName = date("F");
$currentYear = date("Y");
$nextWeek = date("Y-m-d", strtotime("+1 week"));
$nextMonth = date("Y-m-d", strtotime("+1 month"));
$date1 = "2025-01-11";
$date2 = "2025-02-11";
$dateDiff = date_diff(date_create($date1), date_create($date2));
$timezone = date_default_timezone_get(); // Current timezone
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>India Time Zone (IST)</title>
</head>
<body>
<h1>Upload a File</h1>
// Create connection
$conn = new mysqli($servername, $username, $password);
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}