Advance PHP - Complete Question Paper with Detailed Answers
Q.1) Attempt All
1. Enlist the PHP DOM's functions and explain them.
- PHP DOM (Document Object Model) allows manipulating XML and HTML documents.
- Important functions:
- `DOMDocument()`: Creates a new DOM document.
- `load()`: Loads an XML document.
- `loadHTML()`: Loads an HTML document.
- `createElement()`: Creates a new element in the document.
- `appendChild()`: Appends a child node to an element.
- `getElementById()`: Retrieves an element by ID.
- `getElementsByTagName()`: Retrieves elements by tag name.
- `saveXML()`: Outputs or saves the XML as a string.
2. What is an XML Parser? Explain types of XML Parsers in PHP.
- An XML Parser processes XML data to convert it into a usable format.
- PHP provides two main types:
1. **DOM Parser**: Reads the entire XML document into memory and provides tree-based
parsing.
2. **SAX Parser**: Parses XML sequentially, making it more memory-efficient.
3. What are the advantages of AJAX in web applications?
- AJAX (Asynchronous JavaScript and XML) allows dynamic updates without page reloads.
- Advantages:
- **Faster Performance**: Updates only required data instead of reloading the entire page.
- **Reduced Server Load**: Only necessary data is fetched.
- **Improved User Experience**: No page refresh provides a smoother interface.
- **Works with JSON & XML**: Compatible with modern data formats.
4. What is the $_SERVER variable? List common $_SERVER elements.
- $_SERVER is a PHP superglobal array containing server and request information.
- Common elements:
- `$_SERVER['HTTP_USER_AGENT']`: Information about the user's browser.
- `$_SERVER['REQUEST_METHOD']`: Returns GET or POST request type.
- `$_SERVER['REMOTE_ADDR']`: Retrieves the user's IP address.
- `$_SERVER['SERVER_NAME']`: Returns the server hostname.
5. What is a self-processing form? Explain with an example.
- A self-processing form submits data to the same script that processes it.
- Example:
```php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
echo "Hello, " . htmlspecialchars($name);
} else {
?>
<form method="POST" action="">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
<?php } ?>
```
Q.2) Attempt All
1. Explain the difference between GET and POST methods.
- **GET Method**:
- Sends data via the URL.
- Data is visible and limited in size.
- Can be cached and bookmarked.
- **POST Method**:
- Sends data in the request body (hidden).
- More secure for sensitive information.
- Cannot be cached or bookmarked.
2. Explain class and object in PHP with an example.
- A class is a blueprint for creating objects.
- Example:
```php
class Car {
public $brand;
public function __construct($brand) {
$this->brand = $brand;
public function getBrand() {
return $this->brand;
}
}
$myCar = new Car("Toyota");
echo $myCar->getBrand();
```
3. Explain the XML document structure with an example.
- XML Structure:
- Contains a root element.
- Uses hierarchical tags.
- Example:
```xml
<library>
<book>
<title>PHP Basics</title>
<author>John Doe</author>
</book>
</library>
```
4. Explain the AJAX web application model.
- AJAX allows web applications to update content dynamically without refreshing the page.
- Steps:
1. User triggers an event (e.g., button click).
2. JavaScript makes an XMLHttpRequest.
3. Server processes the request and returns data.
4. JavaScript updates the webpage dynamically.
5. Write a PHP program that implements the addition of two numbers using AJAX.
- **HTML + JavaScript**:
```html
<input type="number" id="num1" placeholder="First Number">
<input type="number" id="num2" placeholder="Second Number">
<button onclick="calculateSum()">Add</button>
<p id="result"></p>
<script>
function calculateSum() {
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
var xhr = new XMLHttpRequest();
xhr.open("POST", "add.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onload = function() {
document.getElementById("result").innerHTML = "Result: " + xhr.responseText;
};
xhr.send("num1=" + num1 + "&num2=" + num2);
</script>
```
- **PHP (add.php)**:
```php
<?php
if (isset($_POST['num1']) && isset($_POST['num2'])) {
echo $_POST['num1'] + $_POST['num2'];
?>
```
Q.3) Additional Questions
1. Explain the use of sessions and cookies in PHP.
- **Session**: Stores user data across multiple pages using `session_start()`.
- **Cookie**: Stores small data in the user's browser.
- Example:
```php
setcookie("user", "John", time() + 3600); // Cookie lasts for 1 hour
```
2. How do you connect to a MySQL database using PHP?
- Using MySQLi:
```php
$conn = new mysqli("localhost", "root", "", "database");
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
```
3. What is the difference between `include` and `require`?
- `include()`: Includes a file but does not stop execution if the file is missing.
- `require()`: Includes a file and stops execution if the file is missing.
4. Explain the MVC architecture in PHP.
- Model: Handles data and business logic.
- View: Displays the UI.
- Controller: Manages communication between Model and View.