Web Designing Level 2 Practicals
1. Simple HTML Form & PHP Echo Statement
Code:
<form method="post">
Enter Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "Hello, " . htmlspecialchars($_POST['name']);
?>
Output:
Hello, John (if "John" was entered)
---
2. Arithmetic, Comparison & Logical Operators
Code:
<?php
$a = 10; $b = 5;
echo "Addition: " . ($a + $b);
echo "<br>Comparison (10 > 5): " . var_export($a > $b, true);
echo "<br>Logical (true && false): " . var_export(true && false, true);
?>
Output:
Addition: 15
Comparison (10 > 5): true
Logical (true && false): false
---
3. Input Marks & Display Grades
Code:
<?php
$marks = 85;
$grade = ($marks >= 90) ? "A" : (($marks >= 75) ? "B" : "C");
echo "Marks: $marks, Grade: $grade";
?>
Output:
Marks: 85, Grade: B
---
4. Addition of Two 2x2 Matrices
Code:
<?php
$A = [[1, 2], [3, 4]];
$B = [[5, 6], [7, 8]];
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
echo ($A[$i][$j] + $B[$i][$j]) . " ";
echo "<br>";
?>
Output:
68
10 12
---
5. Factorial using Function
Code:
<?php
function factorial($n) {
return ($n == 0) ? 1 : $n * factorial($n - 1);
echo "Factorial of 5: " . factorial(5);
?>
Output:
Factorial of 5: 120
---
6. String, Date & Math Functions
Code:
<?php
echo "String Length: " . strlen("Hello");
echo "<br>Today's Date: " . date("Y-m-d");
echo "<br>Square Root of 16: " . sqrt(16);
?>
Output:
String Length: 5
Today's Date: 2025-03-09
Square Root of 16: 4
---
7. Student Registration Form
Code:
<form method="post">
Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "Registered: " . htmlspecialchars($_POST['name']);
?>
Output:
Registered: Alice
---
8. Passing Variables via URL
Code:
Page 1:
<a href="page2.php?name=John">Click Here</a>
Page 2 (page2.php):
<?php echo "Hello, " . $_GET['name']; ?>
Output:
Hello, John
---
9. Passing Variables via Sessions
Code:
Page 1:
<?php
session_start();
$_SESSION["user"] = "Alice";
header("Location: page2.php");
?>
Page 2 (page2.php):
<?php
session_start();
echo "Welcome " . $_SESSION["user"];
?>
Output:
Welcome Alice
---
10. Passing Variables via Cookies
Code:
Set Cookie:
<?php
setcookie("user", "Bob", time() + 3600);
?>
Get Cookie:
<?php
echo "Hello, " . $_COOKIE['user'];
?>
Output:
Hello, Bob
---
11. Page Load Counter
Code:
<?php
session_start();
$_SESSION['count'] = ($_SESSION['count'] ?? 0) + 1;
echo "Page Loaded: " . $_SESSION['count'] . " times";
?>
Output:
Page Loaded: 3 times
---
12. Exception Handling
Code:
<?php
try {
echo 5 / 0;
} catch (Exception $e) {
echo "Error: Division by zero";
?>
Output:
Error: Division by zero
---
13. Connect MySQL Server
Code:
<?php
$conn = new mysqli("localhost", "root", "", "test");
if ($conn->connect_error) die("Connection failed");
echo "Connected Successfully";
?>
Output:
Connected Successfully
---
14. Read Employee Table
Code:
<?php
$conn = new mysqli("localhost", "root", "", "test");
$res = $conn->query("SELECT * FROM emp");
while ($row = $res->fetch_assoc()) echo $row['name'] . "<br>";
?>
Output:
John
Alice
Bob
---
15. Create & Insert Customer Table
Code:
<?php
$conn = new mysqli("localhost", "root", "", "test");
$conn->query("CREATE TABLE customer (cust_no INT, cust_name VARCHAR(50))");
$conn->query("INSERT INTO customer VALUES (1, 'Alice')");
?>
Output:
Table Created & Data Inserted
---
16. Read & Display Customer Table
Code:
<?php
$res = $conn->query("SELECT * FROM customer");
while ($row = $res->fetch_assoc()) echo $row['cust_name'] . "<br>";
?>
Output:
Alice
Bob
---
17. Insert Customer Record
Code:
<?php
$conn->query("INSERT INTO customer VALUES (2, 'Bob')");
?>
Output:
Record Inserted
---
18. Update Customer Name
Code:
<?php
$conn->query("UPDATE customer SET cust_name='Bob' WHERE cust_no=1");
?>
Output:
Record Updated
---
19. Delete Customer Record
Code:
<?php
$conn->query("DELETE FROM customer WHERE cust_no=3");
?>
Output:
Record Deleted
---
20. Dynamic Web Page
Code:
<?php
$conn = new mysqli("localhost", "root", "", "test");
$res = $conn->query("SELECT * FROM customer");
while ($row = $res->fetch_assoc()) echo "<h1>" . $row['cust_name'] . "</h1>";
?>
Output:
Alice
Bob