📝 PHP Exam – Answer Sheet
📘 Section A – MCQ Answers (with explanation)
- B – Hypertext Preprocessor
→ Official recursive name of PHP. - B – $
→ All PHP variables start with$. - C – Both
→echoandprintboth output text (echois faster). - C – Fatal Error
→ Stops execution immediately. - B – $_POST
→ Used to collect form data (POST method). - B – mysqli_connect()
→ Procedural way to connect MySQL. - B – Variable exists
→ Checks if variable is set and not null. - D – All
→try,catch,throware used in exception handling. - B – session_start()
→ Initializes session. - 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
| GET | POST |
|---|---|
| Data in URL | Data in body |
| Less secure | More secure |
| Limited size | Large 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
| include | require |
|---|---|
| Warning if file missing | Fatal error |
| Script continues | Script 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






