Php_Unit_Test_Answer_Sheet
Php_Unit_Test_Answer_Sheet
Example:
2. Multiple Inheritance (Using Traits) – PHP doesn’t support multiple inheritance directly
but uses traits.
3. Multilevel Inheritance – A child class inherits from a parent, and another class inherits
from that child.
4. Hierarchical Inheritance – Multiple child classes inherit from the same parent class.
4. Write the difference between get() and post() method of form.
GET vs POST Method:
5. Define introspection.
Introspection in PHP refers to the ability to examine classes, methods, and properties at
runtime. It is useful for debugging and dynamic programming.
- get_class()
- get_methods()
- get_class_vars()
Example:
6. What is mysqli_connect()?
mysqli_connect() is a function used to establish a connection between PHP and a MySQL
database.
Syntax:
Example:
// Establish connection
$conn = mysqli_connect($server, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully!";
?>
2. Describe form controls – text box, text area, radio button, check box, list &
buttons.
1. Text Box (<input type='text'>) – Used for single-line input (e.g., name, email).
2. Text Area (<textarea>) – Used for multi-line input (e.g., comments, feedback).
3. Radio Button (<input type='radio'>) – Used for selecting one option from multiple
choices.
6. Buttons (<input type='submit'>, <input type='reset'>) – Used to submit or reset the form.
Example Form:
<form>
Name: <input type="text" name="name"><br>
Gender: <input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female<br>
Skills: <input type="checkbox" name="skills" value="PHP"> PHP
<input type="checkbox" name="skills" value="MySQL"> MySQL<br>
<input type="submit" value="Submit">
</form>
3. Explain the concept of constructor and destructor in detail.
Constructor:
A constructor is a special function that gets executed automatically when an object is
created. It is defined using the __construct() function.
Example:
class Car {
public $brand;
function __construct($name) {
$this->brand = $name;
echo "Car brand: " . $this->brand;
}
}
$obj = new Car("Toyota");
Destructor:
A destructor is executed automatically when an object is destroyed or script execution ends.
It is defined using __destruct().
Example:
class Car {
function __destruct() {
echo "Object is being destroyed.";
}
}
$obj = new Car();
Creating a Cookie
<?php
setcookie("user", "John", time() + 3600, "/"); // Cookie valid for 1 hour
echo "Cookie has been set!";
?>
Retrieving a Cookie
<?php
if(isset($_COOKIE["user"])) {
echo "Welcome " . $_COOKIE["user"];
} else {
echo "No cookie found!";
}
?>
Deleting a Cookie
<?php
setcookie("user", "", time() - 3600, "/"); // Expire the cookie
echo "Cookie deleted!";
?>