Detailed Answer Sheet (Solutions)

📝 PHP Exam – Answer Sheet


📘 Section A – MCQ Answers (with explanation)

  1. B – Hypertext Preprocessor
    → Official recursive name of PHP.
  2. B – $
    → All PHP variables start with $.
  3. C – Both
    echo and print both output text (echo is faster).
  4. C – Fatal Error
    → Stops execution immediately.
  5. B – $_POST
    → Used to collect form data (POST method).
  6. B – mysqli_connect()
    → Procedural way to connect MySQL.
  7. B – Variable exists
    → Checks if variable is set and not null.
  8. D – All
    try, catch, throw are used in exception handling.
  9. B – session_start()
    → Initializes session.
  10. C – .php
    → Default extension.

📗 Section B – Short Answers

1. What is PHP?

PHP is a server-side scripting language used to build dynamic web applications.


2. GET vs POST

GETPOST
Data in URLData in body
Less secureMore secure
Limited sizeLarge data

3. Session

A session stores user data on the server across multiple pages.


4. Cookie

A cookie stores small data on the user’s browser.


5. Array

An array stores multiple values in one variable.

$arr = ["A", "B", "C"];

6. Error Handling

Process of detecting and managing errors in PHP scripts.


7. include vs require

includerequire
Warning if file missingFatal error
Script continuesScript stops

8. Function

Reusable block of code.

function test() {
echo "Hello";
}

9. $_SERVER

Superglobal containing server/environment info.


10. PDO

PHP Data Objects – secure database access using prepared statements.


📙 Section C – Long Answers

1. PHP Data Types

  • String → "Hello"
  • Integer → 10
  • Float → 10.5
  • Boolean → true/false
  • Array → [1,2,3]
  • Object → Class instance
  • NULL → empty variable

2. Loops in PHP

// for
for ($i=0; $i<5; $i++) {
echo $i;
}// while
$i = 0;
while ($i < 5) {
echo $i;
$i++;
}// foreach
$arr = [1,2,3];
foreach ($arr as $val) {
echo $val;
}

3. Form Handling

<form method="post">
<input type="text" name="name">
<input type="submit">
</form><?php
if ($_POST) {
echo $_POST['name'];
}
?>

4. Error Handling

try {
throw new Exception("Error occurred");
} catch (Exception $e) {
echo $e->getMessage();
}

Also:

  • error_reporting()
  • set_error_handler()

5. Session & Cookie

// Session
session_start();
$_SESSION['user'] = "Aditya";// Cookie
setcookie("user", "Aditya", time()+3600);

💻 Section D – Practical Solutions


✅ Q1. Even/Odd

$num = 10;if ($num % 2 == 0) {
echo "Even";
} else {
echo "Odd";
}

✅ Q2. Form Handling

<form method="post">
<input type="text" name="name">
<input type="submit">
</form><?php
if ($_POST) {
echo htmlspecialchars($_POST['name']);
}
?>

👉 htmlspecialchars() prevents XSS (important improvement)


✅ Q3. Database Connection

$conn = mysqli_connect("localhost", "root", "", "test");if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}echo "Connected successfully";

✅ Q4. Exception Handling

try {
if (!file_exists("test.txt")) {
throw new Exception("File not found");
}
} catch (Exception $e) {
echo $e->getMessage();
}

🏆 Bonus Solution – Login System

session_start();if ($_POST) {
$user = $_POST['username'];
$pass = $_POST['password']; if ($user == "admin" && $pass == "1234") {
$_SESSION['user'] = $user;
echo "Login successful";
} else {
echo "Invalid credentials";
}
}

🎯 Extra Examiner Notes (High Scoring Tips)

✔ Use input validation & sanitization
✔ Prefer PDO over mysqli (security)
✔ Always handle errors properly
✔ Avoid showing raw errors in production
✔ Write clean, readable code