PHP-PR_LISTt
PHP-PR_LISTt
<!DOCTYPE html>
<html>
<body>
<form method="post">
Enter Marks: <input type="number" name="marks">
<input type="submit" value="Check Grade">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$marks = $_POST["marks"];
switch (true) {
case ($marks >= 75):
echo "Grade: A";
break;
case ($marks >= 60):
echo "Grade: B";
break;
case ($marks >= 40):
echo "Grade: C";
break;
default:
echo "Grade: Fail";
}
}
?>
</body>
</html>
<form method="post">
Enter a number: <input type="number" name="num">
<input type="submit" value="Find Sum of Digits">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num = $_POST["num"];
$sum = 0;
</body>
</html>
<form method="post">
Enter a number: <input type="number" name="num">
<input type="submit" value="Show Table">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n = $_POST["num"];
</body>
</html>
<?php
for ($i = 1; $i <= 10; $i++) {
echo "<tr>";
for ($n = 1; $n <= 5; $n++) {
echo "<td>$n x $i = " . ($n * $i) . "</td>";
}
echo "</tr>";
}
?>
</table>
</body>
</html>
6. Write a foreach loop to print values of an indexed and associative array.
<?php
// Indexed array
$colors = ["Red", "Green", "Blue"];
// Associative array
$student = ["name" => "Shravan", "age" => 21, "course" => "Computer Engineering"];
<h3>Student Grades</h3>
<?php
$students = [
"Shravan" => "A",
"Priya" => "B",
"Amit" => "C",
"Neha" => "A+"
];
</body>
</html>
8. Write a program using multidimensional arrays to store student records (Name, Roll
No, Marks) and print them.
<!DOCTYPE html>
<html>
<body>
<h3>Student Records</h3>
<?php
$students = [
["name" => "Shravan", "roll_no" => "101", "marks" => 85],
["name" => "Priya", "roll_no" => "102", "marks" => 78],
["name" => "Amit", "roll_no" => "103", "marks" => 92],
["name" => "Neha", "roll_no" => "104", "marks" => 88]
];
</body>
</html>
9. Write a program using multidimensional arrays to store user records (Name, email id,
mobile no and address) and print them.
<!DOCTYPE html>
<html>
<body>
<form method="post">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
Mobile No: <input type="text" name="mobile" required><br><br>
Address: <textarea name="address" required></textarea><br><br>
<input type="submit" value="Submit">
</form>
<h3>User Records</h3>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$users = [];
</table>
</body>
</html>
10. Write a PHP program to calculate the length of a string and to count words without using
str_word_count().
<!DOCTYPE html>
<html>
<body>
<form method="post">
Enter a string: <input type="text" name="text" required>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$text = $_POST["text"];
</body>
</html>
<form method="post">
Enter first number: <input type="number" name="num1" required><br><br>
Enter second number: <input type="number" name="num2" required><br><br>
<input type="submit" value="Calculate Sum">
</form>
<?php
function calculateSum($a, $b) {
return $a + $b;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n1 = $_POST["num1"];
$n2 = $_POST["num2"];
$sum = calculateSum($n1, $n2);
echo "<h3>Sum: $sum</h3>";
}
?>
</body>
</html>
// Child class
class Test1 extends Student {
public $marks;
14. Implement multilevel inheritance in PHP (parent class: student child class:
Testsresult) and Display result of student for two class test percentage and average of
test1 and test2 subject wise.
<!DOCTYPE html>
<html>
<body>
<h4>Test 1 Marks</h4>
Math: <input type="number" name="test1_math" required><br>
Science: <input type="number" name="test1_science" required><br>
English: <input type="number" name="test1_english"
required><br><br>
<h4>Test 2 Marks</h4>
Math: <input type="number" name="test2_math" required><br>
Science: <input type="number" name="test2_science" required><br>
English: <input type="number" name="test2_english"
required><br><br>
<?php
// Parent class
class Student {
public $name, $roll;
// Child class
class Tests extends Student {
public $test1 = [], $test2 = [];
// Grandchild class
class Result extends Tests {
public function displayResult() {
$this->displayStudent();
$total = 0;
$subjects = array_keys($this->test1);
foreach ($subjects as $subject) {
$mark1 = $this->test1[$subject];
$mark2 = $this->test2[$subject];
$avg = ($mark1 + $mark2) / 2;
$total += $mark1 + $mark2;
echo "<tr>
<td>$subject</td>
<td>$mark1</td>
<td>$mark2</td>
<td>$avg</td>
</tr>";
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$roll = $_POST["roll"];
$test1 = [
"Math" => $_POST["test1_math"],
"Science" => $_POST["test1_science"],
"English" => $_POST["test1_english"]
];
$test2 = [
"Math" => $_POST["test2_math"],
"Science" => $_POST["test2_science"],
"English" => $_POST["test2_english"]
];
</body>
</html>
15. Write a PHP script to demonstrate any five-class introspection using
<!DOCTYPE html>
<html>
<body>
<?php
class Person {
public $name = "Default";
public $age = 0;
function greet() {
echo "Hello!";
}
}
function study() {
echo "Studying...";
}
}
$obj = new Student();
</body>
</html>
16. Create a class product with data members id, name and price. Consider that
customer bought multiple products. Then Calculate the total price of products
purchased. Use constructor
<?php
class Product {
public $id, $name, $price;
$total = 0;
echo "<h3>Purchased Products:</h3>";
foreach ($products as $p) {
$p->display();
$total += $p->price;
}
$unserializedArr = unserialize($serializedArr);
echo "<br>Unserialized Array:<br>";
print_r($unserializedArr);
// Object serialization
class Student {
public $name, $roll;
function __construct($name, $roll) {
$this->name = $name;
$this->roll = $roll;
}
}
$student = new Student("Amit", 102);
$serializedObj = serialize($student);
echo "<br>Serialized Object:<br>$serializedObj<br>";
$unserializedObj = unserialize($serializedObj);
echo "<br>Unserialized Object:<br>";
echo "Name: {$unserializedObj->name}, Roll: {$unserializedObj->roll}";
?>
</body>
</html>
18. Design a form for user registration and validate fields like email, password, and phone
using PHP.
<!DOCTYPE html>
<html>
<body>
<?php
// Define error messages
$emailErr = $passwordErr = $phoneErr = "";
$email = $password = $phone = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate Email
if (empty($_POST["email"])) {
$emailErr = "Email is required.";
} elseif (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format.";
} else {
$email = $_POST["email"];
}
// Validate Password
if (empty($_POST["password"])) {
$passwordErr = "Password is required.";
} elseif (strlen($_POST["password"]) < 6) {
$passwordErr = "Password must be at least 6 characters.";
} else {
$password = $_POST["password"];
}
// Validate Phone
if (empty($_POST["phone"])) {
$phoneErr = "Phone number is required.";
} elseif (!preg_match("/^[0-9]{10}$/", $_POST["phone"])) {
$phoneErr = "Invalid phone number. It should be 10 digits.";
} else {
$phone = $_POST["phone"];
}
</body>
</html>
19. Design a form for user registration and retrieve information on successful submission using
PHP.
20. Develop a PHP application to enter student data into a MySQL database. Retrieve and
display data from the database in tabular form. Update a record in the database. Delete a
specific record from the database.
21. Develop a PHP application to enter student data into a MySQL database. Retrieve and
display data from the database in tabular form. Delete a specific record from the database.
<!DOCTYPE html>
<html>
<body>
<h3>Student Data Management</h3>
<?php
// Database connection
$servername = "localhost";
$username = "root"; // Use your database username
$password = ""; // Use your database password
$dbname = "student_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Display Students
$sql = "SELECT * FROM students";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['roll_no'] . "</td>
<td>" . $row['email'] . "</td>
<td>
<a href='?edit=" . $row['id'] . "'>Edit</a> |
<a href='?delete=" . $row['id'] . "'>Delete</a>
</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
<?php
// Edit Student Form
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM students WHERE id=$id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
</body>
</html>
22.