0% found this document useful (0 votes)
6 views

Php_Unit_Test_Answer_Sheet

The document is a PHP Unit Test Paper that includes questions on MySQL data types, cookie management, inheritance types, form methods, introspection, and mysqli_connect(). It also contains programming tasks such as connecting PHP with MySQL, describing form controls, and explaining constructors and destructors. Additionally, it provides examples of cookie creation, retrieval, and deletion in PHP.

Uploaded by

jayankeni2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Php_Unit_Test_Answer_Sheet

The document is a PHP Unit Test Paper that includes questions on MySQL data types, cookie management, inheritance types, form methods, introspection, and mysqli_connect(). It also contains programming tasks such as connecting PHP with MySQL, describing form controls, and explaining constructors and destructors. Additionally, it provides examples of cookie creation, retrieval, and deletion in PHP.

Uploaded by

jayankeni2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PHP Unit Test Paper - Answer Sheet

Q1 (2 Marks Each - Any 4 out of 6)

1. List any four data types in MySQL.


MySQL provides several data types. Four of them are:

- INT – Used for whole numbers (e.g., 10, 25, -50).

- VARCHAR – Used for variable-length text (e.g., names, addresses).

- DATE – Used for storing dates (e.g., '2024-07-01').

- FLOAT – Used for decimal numbers (e.g., 3.14, 9.99).

2. How can we destroy cookies?


We can destroy a cookie in PHP by setting its expiration time to a past date using the
setcookie() function.

Example:

setcookie("username", "", time() - 3600, "/"); // Cookie expires in the past

3. List types of inheritance.


PHP supports the following types of inheritance:

1. Single Inheritance – A class inherits from only one parent class.

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.

PHP provides functions for introspection, such as:

- get_class()

- get_methods()

- get_class_vars()

Example:

class Car { public $brand = "Toyota"; }

$obj = new Car(); echo get_class($obj); // Output: Car

6. What is mysqli_connect()?
mysqli_connect() is a function used to establish a connection between PHP and a MySQL
database.

Syntax:

$connection = mysqli_connect("hostname", "username", "password", "database_name");

Example:

$conn = mysqli_connect("localhost", "root", "", "test_db");

if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }

echo "Connected successfully!";


Q2 (4 Marks Each - Any 3 out of 4)

1. Write a program to connect PHP with MySQL.


<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "test_db";

// 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.

4. Check Box (<input type='checkbox'>) – Used for selecting multiple options.

5. List (<select>) – Dropdown selection menu.

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();

4. Write a PHP program to demonstrate the use of cookies.

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!";
?>

You might also like