Experiment 1:
1. Write a program to create Student registration form
<!DOCTYPE html>
<html>
<head>
<title>Student Registration Form</title>
</head>
<body>
<h2>Student Registration Form</h2>
<form method="post" action="register.php">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<label for="phone">Phone:</label><br>
<input type="text" id="phone" name="phone" required><br><br>
<label for="dob">Date of Birth:</label><br>
<input type="date" id="dob" name="dob" required><br><br>
<label for="gender">Gender:</label><br>
<input type="radio" id="male" name="gender" value="Male" required>
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="Female">
<label for="female">Female</label><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$dob = $_POST['dob'];
$gender = $_POST['gender'];
// Process the data further (e.g., store it in a database)
// For demonstration, let's just print the data
echo "Name: $name <br>";
echo "Email: $email <br>";
echo "Phone: $phone <br>";
echo "Date of Birth: $dob <br>";
echo "Gender: $gender <br>";
} else
{
// If the form is not submitted, redirect to the form page
header("Location: student_registration_form.php");
exit();
}
?>
Experiment 2:
2. Write a program to perform EB bill calculation
<?php
// Function to calculate electricity bill
function calculateElectricityBill($units) {
$totalAmount = 0;
// Define tariff rates
$firstSlabRate = 3.50;
$secondSlabRate = 4.50;
$thirdSlabRate = 5.50;
// Define slab limits
$firstSlabLimit = 100;
$secondSlabLimit = 200;
// Calculate bill based on slabs
if ($units <= $firstSlabLimit) {
$totalAmount = $units * $firstSlabRate;
} elseif ($units > $firstSlabLimit && $units <= $secondSlabLimit) {
$totalAmount = $firstSlabLimit * $firstSlabRate + ($units - $firstSlabLimit) * $secondSlabRate;
} else {
$totalAmount = $firstSlabLimit * $firstSlabRate + ($secondSlabLimit - $firstSlabLimit) * $secondSlabRate +
($units - $secondSlabLimit) * $thirdSlabRate;
} return $totalAmount;
}// Example usage
$unitsConsumed = 250; // Change this value to test with different consumption
$totalBillAmount = calculateElectricityBill($unitsConsumed);
echo "Total Electricity Bill for $unitsConsumed units: $totalBillAmount";
?>
Experiment 3:
3. Write a program to perform Student grade manipulation
<?php
// Function to calculate grade based on marks
function calculateGrade($marks) {
if ($marks >= 90) {
return 'A+';
} elseif ($marks >= 80 && $marks < 90) {
return 'A';
} elseif ($marks >= 70 && $marks < 80) {
return 'B';
} elseif ($marks >= 60 && $marks < 70) {
return 'C';
} elseif ($marks >= 50 && $marks < 60) {
return 'D';
} else {
return 'F';
}// Function to calculate GPA based on grade
function calculateGPA($grade) {
switch ($grade) {
case 'A+':
return 4.0;
case 'A':
return 4.0;
case 'B':
return 3.5;
case 'C':
return 3.0;
case 'D':
return 2.5;
default:
return 0.0;
// Example usage
$studentMarks = 78; // Change this value to test with different marks
$studentGrade = calculateGrade($studentMarks);
$studentGPA = calculateGPA($studentGrade);
echo "Student Marks: $studentMarks\n";
echo "Student Grade: $studentGrade\n";
echo "Student GPA: $studentGPA";
?>
Experiment 4:
4. Write a program to perform String operations in PHP.
<?php
// Example strings
$string1 = "Hello,";
$string2 = "World!";
$string3 = " This is a string with spaces. ";
$string4 = "apple,orange,banana";
// Concatenation
$concatenatedString = $string1 . " " . $string2;
echo "Concatenated String: $concatenatedString\n";
// String length
$length = strlen($string1);
echo "Length of '$string1': $length\n";
// Substring
$substring = substr($string2, 0, 3);
echo "Substring of '$string2': $substring\n";
// Uppercase and lowercase
$uppercaseString = strtoupper($string1);
$lowercaseString = strtolower($string2);
echo "Uppercase of '$string1': $uppercaseString\n";
echo "Lowercase of '$string2': $lowercaseString\n";
// Trim spaces
$trimmedString = trim($string3);
echo "Trimmed String: '$trimmedString'\n";
// Explode string
$explodedArray = explode(",", $string4);
echo "Exploded Array: ";
print_r($explodedArray);
// Implode array
$implodedString = implode("-", $explodedArray);
echo "Imploded String: $implodedString\n";
// Replace string
$replacedString = str_replace("string", "sentence", $string3);
echo "Replaced String: $replacedString\n";
?>
Experiment 5:
5. Write a program to create Book master form.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Book Master Form</title>
</head>
<body>
<h2>Book Master Form</h2>
<form action="process_book.php" method="post">
<label for="title">Title:</label><br>
<input type="text" id="title" name="title" required><br><br>
<label for="author">Author:</label><br>
<input type="text" id="author" name="author" required><br><br>
<label for="genre">Genre:</label><br>
<input type="text" id="genre" name="genre" required><br><br>
<label for="year">Year:</label><br>
<input type="number" id="year" name="year" min="1800" max="<?php echo date('Y'); ?>"
required><br><br>
<label for="price">Price:</label><br>
<input type="number" id="price" name="price" step="0.01" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$title = $_POST["title"];
$author = $_POST["author"];
$genre = $_POST["genre"];
$year = $_POST["year"];
$price = $_POST["price"];
// You can perform validation on form data here
// Display the submitted book details
echo "<h2>Book Details</h2>";
echo "<p><strong>Title:</strong> $title</p>";
echo "<p><strong>Author:</strong> $author</p>";
echo "<p><strong>Genre:</strong> $genre</p>";
echo "<p><strong>Year:</strong> $year</p>";
echo "<p><strong>Price:</strong> $price</p>";
?>
Experiment 6:
6. Write a program to perform Form validation – Railway ticket reservation.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Railway Ticket Reservation</title>
</head>
<body>
<h2>Railway Ticket Reservation</h2>
<form action="process_reservation.php" method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<label for="from">From:</label><br>
<input type="text" id="from" name="from" required><br><br>
<label for="to">To:</label><br>
<input type="text" id="to" name="to" required><br><br>
<label for="date">Date:</label><br>
<input type="date" id="date" name="date" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$name = $_POST["name"];
$email = $_POST["email"];
$from = $_POST["from"];
$to = $_POST["to"];
$date = $_POST["date"];
// Validate form data
$errors = [];
if (empty($name)) {
$errors[] = "Name is required";
}
if (empty($email)) {
$errors[] = "Email is required";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format";
}
if (empty($from)) {
$errors[] = "Departure station is required";
}
if (empty($to)) {
$errors[] = "Destination station is required";
}
if (empty($date)) {
$errors[] = "Date of travel is required";
}
// Display errors or process reservation
if (!empty($errors)) {
echo "<h2>Form Validation Errors:</h2>";
foreach ($errors as $error) {
echo "<p>$error</p>";
}
} else {
// Process reservation (you can add further processing here)
echo "<h2>Reservation Successful!</h2>";
echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
echo "<p>From: $from</p>";
echo "<p>To: $to</p>";
echo "<p>Date: $date</p>";
}
}
?>
Experiment 7:
7. Write a program to perform Date and time operations in PHP.
<?php
// Get current date and time
$currentDateTime = date("Y-m-d H:i:s");
echo "Current Date and Time: $currentDateTime\n";
// Get current date
$currentDate = date("Y-m-d");
ech "Current Date: $currentDate\n";
// Get current time
$currentTime = date("H:i:s");
echo "Current Time: $currentTime\n";
// Create a DateTime object for a specific date and time
$dateTime = new DateTime("2024-04-26 15:30:00");
echo "Specific Date and Time: " . $dateTime->format("Y-m-d H:i:s") . "\n";
// Format a date
$dateToFormat = "2024-04-26";
$formattedDate = date("F j, Y", strtotime($dateToFormat));
echo "Formatted Date: $formattedDate\n";
// Add days to a date
$dateToIncrement = "2024-04-26";
$daysToAdd = 5;
$newDate = date("Y-m-d", strtotime($dateToIncrement . " + $daysToAdd days"));
echo "Date after adding $daysToAdd days: $newDate\n";
// Calculate difference between two dates
$date1 = new DateTime("2024-04-26");
$date2 = new DateTime("2024-05-01");
$interval = $date1->diff($date2);
eco "Difference between $date1 and $date2: " . $interval->days . " days\n";
?>
Experiment 8:
8. Write a program to Identify the web browser.
<?php
// Get user agent string
$userAgent = $_SERVER['HTTP_USER_AGENT'];
// Initialize browser variable
$browser = "Unknown";
// Check for common browser strings
if (strpos($userAgent, 'MSIE') !== false || strpos($userAgent, 'Trident/') !== false) {
$browser = "Internet Explorer";
} elseif (strpos($userAgent, 'Firefox') !== false) {
$browser = "Mozilla Firefox";
} elseif (strpos($userAgent, 'Chrome') !== false) {
$browser = "Google Chrome";
} elseif (strpos($userAgent, 'Safari') !== false) {
$browser = "Apple Safari";
} elseif (strpos($userAgent, 'Opera') !== false || strpos($userAgent, 'OPR/') !== false) {
$browser = "Opera";
} elseif (strpos($userAgent, 'Edge') !== false) {
$browser = "Microsoft Edge";
// Output the detected browser
echo "Detected Browser: $browser";
?>