1. Write a PHP program to Display “Hello”, and today’s date.
<!DOCTYPE html>
<html>
<head>
<title>Hello and Date</title>
</head>
<body>
<?php
echo "Hello";
echo "<br>";
echo "Today's date is: " . date("Y-m-d");
?>
</body>
</html>
2. Write a PHP program to display Fibonacci series.
<?php
// Check if the form has been submitted
if (isset($_POST['submit'])) {
// Get the number of terms from the form input
$num_terms = intval($_POST['num_terms']);
// Validate the input
if ($num_terms <= 0) {
$error_message = "Please enter a positive integer for the number of terms.";
} else {
// Initialize the first two Fibonacci numbers
$first_num = 0;
$second_num = 1;
$fib_series = [];
// Add the first two numbers if num_terms is at least 1 or 2
if ($num_terms >= 1) {
$fib_series[] = $first_num;
}
if ($num_terms >= 2) {
$fib_series[] = $second_num;
}
// Generate the Fibonacci series
for ($i = 2; $i < $num_terms; $i++) {
$next_num = $first_num + $second_num;
$fib_series[] = $next_num;
$first_num = $second_num;
$second_num = $next_num;
}
$result_message = "Fibonacci series for " . $num_terms . " terms: " . implode(" ",
$fib_series);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>
<h1>Fibonacci Series Generator</h1>
<form method="post" action="">
<label for="num_terms">Enter the number of terms:</label><br>
<input type="number" id="num_terms" name="num_terms" min="1" required><br><br>
<input type="submit" name="submit" value="Generate Series">
</form>
<?php
// Display error or result messages
if (isset($error_message)) {
echo "<p style='color: red;'>" . $error_message . "</p>";
} elseif (isset($result_message)) {
echo "<h2>" . $result_message . "</h2>";
}
?>
</body>
</html>
3. Write a PHP Program to read the employee details.
employee_form.html
<!DOCTYPE html>
<html>
<head>
<title>Employee Details Form</title>
</head>
<body>
<h2>Enter Employee Details</h2>
<form action="process_employee.php" method="POST">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>
<label for="employee_id">Employee ID:</label><br>
<input type="text" id="employee_id" name="employee_id" required><br><br>
<label for="department">Department:</label><br>
<input type="text" id="department" name="department"><br><br>
<label for="salary">Salary:</label><br>
<input type="number" id="salary" name="salary" step="0.01"><br><br>
<input type="submit" value="Submit Details">
</form>
</body>
</html>
process_employee.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect form data using $_POST superglobal
$name = $_POST['name'];
$employee_id = $_POST['employee_id'];
$department = $_POST['department'];
$salary = $_POST['salary'];
echo "<h2>Employee Details Received:</h2>";
echo "<p><strong>Name:</strong> " . htmlspecialchars($name) . "</p>";
echo "<p><strong>Employee ID:</strong> " . htmlspecialchars($employee_id) . "</p>";
echo "<p><strong>Department:</strong> " . htmlspecialchars($department) . "</p>";
echo "<p><strong>Salary:</strong> " . htmlspecialchars($salary) . "</p>";
}
else
{
echo "<p>No data submitted.</p>";
}
?>
4. Write a PHP program to prepare the student marks list.
<?php
// Define an array to store student data
$students = [
[
"name" => "SANIYA",
"roll_no" => "S001",
"marks" => ["PHP & MYSQL" => 95, "ML" => 92, "WEB PROGRAMMING" => 90]
],
[
"name" => "MOUNIKA",
"roll_no" => "S002",
"marks" => ["PHP & MYSQL" => 90, "ML" => 88, "WEB PROGRAMMING" => 95]
],
[
"name" => "KHAJA",
"roll_no" => "S003",
"marks" => ["PHP & MYSQL" => 95, "ML" => 75, "WEB PROGRAMMING" => 90]
],
];
echo "<h2>Student Marks List</h2>";
echo "<table border='1' cellpadding='5' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<th>Roll No</th>";
echo "<th>Name</th>";
echo "<th>Math</th>";
echo "<th>Science</th>";
echo "<th>English</th>";
echo "<th>Total Marks</th>";
echo "<th>Percentage (%)</th>";
echo "<th>Grade</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($students as $student) {
$total_marks = array_sum($student["marks"]);
$num_subjects = count($student["marks"]);
$percentage = ($total_marks / ($num_subjects * 100)) * 100;
// Determine grade based on percentage
if ($percentage >= 90) {
$grade = "A";
} elseif ($percentage >= 80) {
$grade = "B";
} elseif ($percentage >= 70) {
$grade = "C";
} elseif ($percentage >= 60) {
$grade = "D";
} else {
$grade = "F";
}
echo "<tr>";
echo "<td>" . htmlspecialchars($student["roll_no"]) . "</td>";
echo "<td>" . htmlspecialchars($student["name"]) . "</td>";
echo "<td>" . htmlspecialchars($student["marks"]["PHP & MYSQL"]) . "</td>";
echo "<td>" . htmlspecialchars($student["marks"]["ML"]) . "</td>";
echo "<td>" . htmlspecialchars($student["marks"]["WEB PROGRAMMING"]) . "</td>";
echo "<td>" . htmlspecialchars($total_marks) . "</td>";
echo "<td>" . number_format($percentage, 2) . "</td>";
echo "<td>" . htmlspecialchars($grade) . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
5. Write a PHP program to generate the multiplication of two matrices.
<?php
// Define first matrix (2x3)
$matrix1 = [
[1, 2, 3],
[4, 5, 6]
];
// Define second matrix (3x2)
$matrix2 = [
[7, 8],
[9, 10],
[11, 12]
];
// Initialize result matrix (2x2)
$result = [
[0, 0],
[0, 0]
];
// Perform matrix multiplication
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
for ($k = 0; $k < 3; $k++) {
$result[$i][$j] += $matrix1[$i][$k] * $matrix2[$k][$j];
}
}
}
// Display the matrices and result
echo "<h2>Matrix 1 (2x3):</h2>";
echo "<pre>";
printMatrix($matrix1);
echo "</pre>";
echo "<h2>Matrix 2 (3x2):</h2>";
echo "<pre>";
printMatrix($matrix2);
echo "</pre>";
echo "<h2>Resultant Matrix (2x2):</h2>";
echo "<pre>";
printMatrix($result);
echo "</pre>";
// Function to print a matrix
function printMatrix($matrix) {
foreach ($matrix as $row)
{
foreach ($row as $value)
{
echo $value."\t";
}
echo "<br>";
}
}
?>
6. Create student registration form using text box, check box, radio button, select,
submit button. And display user inserted value in new PHP page.
Register.html
<!DOCTYPE html>
<html>
<head>
<title>Student Registration Form</title>
</head>
<body>
<h2>Student Registration Form</h2>
<form action="display.php" method="POST">
<label for="name">Name:</label>
<input type="text" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" name="email" required><br><br>
<label for="phone">Phone:</label>
<input type="text" name="phone" required><br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="Male" required> Male
<input type="radio" name="gender" value="Female"> Female<br><br>
<label>Courses:</label><br>
<input type="checkbox" name="courses[]" value="C"> C<br>
<input type="checkbox" name="courses[]" value="C++"> C++<br>
<input type="checkbox" name="courses[]" value="Java"> Java<br>
<input type="checkbox" name="courses[]" value="Python"> Python<br><br>
<label for="department">Department:</label>
<select name="department" required>
<option value="">--Select Department--</option>
<option value="BCA">BCA</option>
<option value="B.Sc Computer Science">B.Sc Computer Science</option>
<option value="B.Com">B.Com</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Display.php
<!DOCTYPE html>
<html>
<head>
<title>Registration Details</title>
</head>
<body>
<h2>Student Registration Details</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST["name"]);
$email = htmlspecialchars($_POST["email"]);
$phone = htmlspecialchars($_POST["phone"]);
$gender = htmlspecialchars($_POST["gender"]);
$department = htmlspecialchars($_POST["department"]);
$courses = isset($_POST["courses"]) ? $_POST["courses"] : [];
echo "<strong>Name:</strong> $name <br>";
echo "<strong>Email:</strong> $email <br>";
echo "<strong>Phone:</strong> $phone <br>";
echo "<strong>Gender:</strong> $gender <br>";
echo "<strong>Department:</strong> $department <br>";
echo "<strong>Courses Selected:</strong> ";
if (!empty($courses)) {
echo implode(", ", $courses);
} else {
echo "None";
}
} else {
echo "Form not submitted!";
}
?>
</body>
</html>
7. Create Website Registration Form using text box, check box, radio button, select, submit
button. And display user inserted value in new PHP page.
Regs.html
<!DOCTYPE html>
<html>
<head>
<title>Website Registration Form</title>
</head>
<body>
<h2>Website Registration Form</h2>
<form action="display1.php" method="post">
<!-- Textbox -->
Name: <input type="text" name="name" required><br><br>
Email: <input type="text" name="email" required><br><br>
<!-- Radio button -->
Gender:
<input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female <br><br>
<!-- Checkbox -->
Hobbies:
<input type="checkbox" name="hobbies[]" value="Reading"> Reading
<input type="checkbox" name="hobbies[]" value="Sports"> Sports
<input type="checkbox" name="hobbies[]" value="Music"> Music <br><br>
<!-- Select -->
Country:
<select name="country">
<option value="India">India</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
</select><br><br>
<!-- Submit -->
<input type="submit" value="Register">
</form>
</body>
</html>
display1.php
<?php
// Collect form data
$name = $_POST['name'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$country = $_POST['country'];
$hobbies = $_POST['hobbies'];
echo "<h2>Registration Details</h2>";
echo "Name: " . $name . "<br>";
echo "Email: " . $email . "<br>";
echo "Gender: " . $gender . "<br>";
echo "Country: " . $country . "<br>";
echo "Hobbies: ";
if(!empty($hobbies)){
foreach($hobbies as $hobby){
echo $hobby . " ";
}
} else {
echo "No hobbies selected.";
}
?>
8. Write PHP script to demonstrate passing variables with cookies.
Step 1: setcookie.php
<?php
// setcookie(name, value, expire_time, path)
$studentName = "Mounika";
$studentCourse = "BCA Bachelor of Application";
// Cookie will expire in 1 hour (3600 seconds)
setcookie("name", $studentName, time() + 3600, "/");
setcookie("course", $studentCourse, time() + 3600, "/");
echo "Cookies are set successfully.<br>";
echo "Go to <a href='getcookie.php'>getcookie.php</a> to view stored cookies.";
?>
Step 2: getcookie.php
<?php
if(isset($_COOKIE["name"]) && isset($_COOKIE["course"])) {
echo "Student Name: " . $_COOKIE["name"] . "<br>";
echo "Course: " . $_COOKIE["course"] . "<br>";
} else {
echo "Cookies are not set yet. Please run setcookie.php first.";
}
?>
9. Write a program to keep track of how many times a visitor has loaded the page.
<?php
// Check if 'visits' cookie exists
if(isset($_COOKIE["visits"])) {
$visits = $_COOKIE["visits"] + 1;
} else {
$visits = 1; // First visit
}
// Update the cookie (valid for 1 hour)
setcookie("visits", $visits, time() + 3600, "/");
// Display the number of visits
echo "<h2>Page Visit Counter</h2>";
echo "You have visited this page <b>" . $visits . "</b> times.";
?>