9. Explain how PHP handles directories. List common directory functions.
PHP provides built-in functions for working with directories. These allow reading, creating, deleting,
and scanning directories.
Common functions:
- opendir() - Opens a directory handle.
- readdir() - Reads entry from directory handle.
- closedir() - Closes a directory handle.
- mkdir() - Creates a new directory.
- rmdir() - Removes a directory.
- scandir() - Returns an array of files and directories inside a specified path.
10. Write a PHP script to insert and retrieve student records from MySQL database.
<?php
$conn = new mysqli("localhost", "root", "", "school");
$sql = "INSERT INTO students (name, age) VALUES ('John', 20)";
$conn->query($sql);
$result = $conn->query("SELECT * FROM students");
while($row = $result->fetch_assoc()) {
echo "Name: " . $row['name'] . ", Age: " . $row['age'] . "<br>";
$conn->close();
?>
11. What is a cookie in PHP? Explain its advantages and disadvantages.
A cookie is a small file stored on the user's computer by the browser to maintain user-specific
information.
Advantages:
- Maintains user sessions across pages.
- Easy to implement.
Disadvantages:
- Limited size (around 4KB).
- Can be disabled by users.
- Less secure than server-side storage.
12. What is the purpose of file handling in PHP? Mention different file modes.
File handling allows reading, writing, and modifying files.
Common file modes:
- 'r' - Read only.
- 'w' - Write only, truncates file.
- 'a' - Append, write only.
- 'r+' - Read and write.
- 'w+' - Read and write, truncates file.
- 'a+' - Read and append.
13. What are HTML form elements? Describe any three commonly used input types.
HTML form elements collect user inputs. Examples:
- <input type="text"> - For single-line text input.
- <input type="email"> - For email addresses.
- <input type="submit"> - For submitting the form.
14. What is recursion? Explain with an example (without code).
Recursion is a programming technique where a function calls itself to solve a smaller part of the
problem.
For example, calculating factorial:
Factorial of 4 = 4 × 3 × 2 × 1 - The function calls itself with smaller values until it reaches 1.
15. Describe the use of conditional statements in PHP. Give examples.
Conditional statements control the flow of code based on conditions.
Example:
if ($age >= 18) {
echo "Adult";
} elseif ($age > 12) {
echo "Teenager";
} else {
echo "Child";