0% found this document useful (0 votes)
18 views4 pages

WBP Ut2

The document provides an overview of key concepts in PHP, including the role of constructors, cookie management, database operations, and data types in MySQL. It also explains inheritance types, introspection, and the differences between sessions and cookies, along with examples and code snippets. Additionally, it includes a PHP program demonstrating a delete operation on database table data.

Uploaded by

riddish09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

WBP Ut2

The document provides an overview of key concepts in PHP, including the role of constructors, cookie management, database operations, and data types in MySQL. It also explains inheritance types, introspection, and the differences between sessions and cookies, along with examples and code snippets. Additionally, it includes a PHP program demonstrating a delete operation on database table data.

Uploaded by

riddish09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

UT2 WBP(PHP)

Q1) ANY 4
1. State the Role of Constructor.
Ans: A constructor is a special method in object-oriented programming that initializes an
object when it is created.
It automatically executes when an instance of a class is created.

2. How can we destroy cookies?


Ans: i) Cookies can be destroyed in PHP by using the setcookie() function:
ii) Ex: setcookie ("user", "", time() - 3600, "/");
iii) Explanation:
• "user" → Name of the cookie.
• "" → Empty value.
• time () - 3600 → Sets expiry time to 1 hour in the past.
• "/" → Ensures the cookie is removed from all paths in the domain.

3. List any four database operations.


1. CREATE – Insert new records into a table.
2. SELECT – Retrieve data from a table.
3. UPDATE – Modify existing data in a table.
4. DELETE – Remove data from a table.

4. List any four datatypes in MySQL.


Ans: i) INT
ii) VARCHAR
iii) DATE
iv) TEXT
v) ENUM
vi) DATE
vii) TIME
viii) TEXT

5. Define Inheritance and its Types.


Ans: Inheritance in PHP is a mechanism where one class derives properties and methods
from another class.
It helps in code reusability and hierarchical structuring.
Types of Inheritance in PHP:
1. Single Inheritance – A child class inherits from a single parent class.
2. Multiple Inheritance– A child class implements multiple interfaces.
3. Multilevel Inheritance – A class inherits from another derived class.
4. Hierarchical Inheritance – Multiple child classes inherit from a single parent class.
6. What is Introspection?
Ans: Introspection in PHP is the ability to examine the properties and methods of a class at
runtime. PHP provides built-in functions for introspection.
Common Introspection Functions:
• get_class($obj) – Returns the class name of an object.
• get_methods($obj) – Returns an array of all methods of an object.
• get_class_vars('ClassName') – Returns class properties.

Q2) ANY 3
1. Explain class and object with a suitable example in PHP.
Ans:
i. Class:
• A class is a template that defines attributes (variables) and behaviors (methods).
• It is created using the class keyword in PHP.
• Syntax: class class_name{
// attribute and behaviors
}
ii. Object:
• An object is an instance of a class.
• It is created using the new keyword.
• Syntax: $object_name = new class_name();
Example:
<html>
<body>
<?php
// Defining a class
class Car {
public $brand;
public $color;

// Constructor
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
public function displayInfo() {
echo "This car is a " . $this->color . " " . $this->brand . ".";
}
}

// Creating an object of the Car class


$myCar = new Car("Toyota", "Red");

// Calling the method


$myCar->displayInfo();
?>
</body>
</html>
2. Write the Difference between GET() and POST() method in PHP.

3. Define session and cookie. Explain the use of session_start().


i) Cookie
• A cookie is a small piece of data stored on the client’s browser.
• It is used to remember user information (like username, preferences, or session ID)
across visits.
• Cookies are set using the setcookie() function in PHP and are automatically sent with
every HTTP request to the server.
• Syntax: setcookie("username", "John", time() + 3600, "/"); // Expires in 1 hour
ii) What is a Session?
• A session is a way to store user data on the server.
• Unlike cookies, session data is not visible to users and is more secure.
• Each user gets a unique session ID, usually stored in a cookie called PHPSESSID.
Use of session_start()
• session_start() is a built-in PHP function used to start a session.
• It must be called at the beginning of the script before any HTML output.
• It enables the use of $_SESSION superglobal to store and retrieve session variables.
Example:
<?php
session_start(); // Starts the session
// Storing session data
$_SESSION["username"] = "JohnDoe";
?>
<html><body><?php
// Retrieving session data
echo "Welcome, " . $_SESSION["username"];
?>
</body>
</html>
4. Write a PHP program to demonstrate delete operation on table data.
<?php
// Step 1: Connect to MySQL Database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stud";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Step 2: Check connection


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Step 3: DELETE query


$student_id = 1; // ID of the student to delete
$sql = "DELETE FROM students WHERE id = $student_id";

// Step 4: Execute query


if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully.";
} else {
echo "Cannot deleted record”;
}

// Step 5: Close connection


$conn->close();
?>

O/P=>
Record deleted successfully.

You might also like