0% found this document useful (0 votes)
36 views7 pages

Imp Question Bank PHP CT22

ct

Uploaded by

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

Imp Question Bank PHP CT22

ct

Uploaded by

spandaa257
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

List introspection methods in detail


ANS:
 Introspection in PHP offers the useful ability to examine an object's characteristics, such as its
name, parent class properties, classes, interfaces and methods.
 PHP offers a large number functions that can be used to accomplish the above task.
o class_exists() → Checks whether a class has been defined.
o get_class() → Returns the class name of an object.
o get_parent_class() → Returns the class name of an object's parent class.
o method_exists() → Checks whether an object defines a specific method.
<?php
class Animal {
public function speak() {}
}

class Dog extends Animal {


public function bark() {}
}

if (class_exists('Dog')) {
$d = new Dog();
echo "Class Name: " . get_class($d) . "\n";
echo "Parent Class: " . get_parent_class($d) . "\n";

if (method_exists($d, 'bark')) {
echo "Method 'bark' exists.\n";
}
}
?>
Op: Class Name: Dog
Parent Class: Animal
Method 'bark' exists.

2. Explain any four features of MySQL?


Ans:
 Open Source
MySQL is free to use and open-source, which means its source code is publicly available
and can be modified to suit user needs.
 High Performance
MySQL provides fast data processing through efficient storage engines and query
optimization techniques.
 Cross-Platform Support
It runs on various operating systems like Windows, Linux, and macOS, making it flexible
and widely usable.
 Security
MySQL offers strong security features such as user authentication, access control, and
SSL support to ensure safe data handling.
3. What are the roles of server in PHP?
ANS:
PHP is a server-side scripting language used to create dynamic and interactive web
applications. The server plays a key role in executing PHP code. Its roles include:
1. The server processes PHP scripts on the backend before sending the output (usually
HTML) to the client's browser.
2. It manages communication between the PHP script and the database (like MySQL),
handling data storage, retrieval, and updates.
3. It controls user access, sessions, and authentication by managing cookies and session
variables.
4. It handles file operations such as uploading, downloading, reading, and writing files on the
server.

4. Describe any four data types of MySQL with its size?


ANS:
MySQL supports different data types to store values like numbers, text, and dates. Four common
data types are:
1. INT – Stores whole numbers, size: 4 bytes
2. VARCHAR(n) – Stores variable-length text, size: up to 65,535 bytes
3. DATE – Stores date in YYYY-MM-DD format, size: 3 bytes
4. FLOAT(m,d) – Stores decimal numbers, size: 4 bytes

5. Define the GET and POST methods


ANS:
GET and POST are two HTTP request methods used to send data from a client (like a browser) to
a server in web applications.
 GET Method:
Sends data through the URL. It is visible, limited in length, and mainly used for retrieving
data.
 POST Method:
Sends data in the request body. It is more secure, supports large amounts of data, and is
commonly used for form submissions.

Example:

 GET → [Link]?name=John
 POST → Used when submitting a login form (username & password sent securely)

6. Difference between cookies and session.


Sr.
No Cookies Sessions
.
Cookies are small text files stored on Sessions store user data on the server and
1
the client’s browser to track user data. are used to manage user state.
Stored on the user’s device (client-
2 Stored on the server (server-side).
side).
Expire after a set time defined by the End when the user logs out or closes the
3
user or developer. browser.
4 Less secure due to client-side storage. More secure as data remains on the server.
Can store large amounts of data (up to
5 Can store up to 4 KB of data.
around 128 MB by default).
6 No need to call a function to use Requires session_start() to begin a
cookies. session.

7. Define the uses of serialize () and unserialize().


ANS:
 serialize()
Converts a PHP value (like an array or object) into a storable string format. This is
useful for saving complex data in files or databases.
 unserialize()
Converts the serialized string back into the original PHP value (array/object).
 $data = ["name" => "Shravan", "age" => 22];
$serialized = serialize($data);
echo $serialized; // Outputs a string

$original = unserialize($serialized);
print_r($original); // Outputs the original array

8. Write short note on interface with example.


1. ANS: An interface allows users to define only the method names (prototypes) that a class
must implement, without including the method logic.
2. It is declared using the interface keyword and cannot have variables or method
implementations—only public abstract methods.
3. All methods in an interface must be public, and a class can implement multiple interfaces
(unlike classes which support only single inheritance).
4. Interfaces are useful for modeling multiple inheritance and enforcing a common structure
across different classes.
SYNTAX1 :
interface InterfaceName {
public function method1();
public function method2();
}
Ex:
interface Animal {
public function makeSound();
}

class Dog implements Animal {


public function makeSound() {
echo "Bark";
}
}

9. State the use of cloning of an object.


ANS:
 Cloning in PHP is the process of creating a copy of an existing object using the clone
keyword.
 Uses:
 To duplicate an object with the same properties without affecting the original.
 To create backups or temporary copies of objects during operations.
 class Car {
public $model = "BMW";
}
$car1 = new Car();
$car2 = clone $car1;

echo $car2->model; // Output: BMW


10. Explain introspection and serialization with example.
ANS:
Introspection:
1. Introspection is the ability of PHP to examine objects, classes, and methods at runtime.
2. It helps in checking class names, methods, or parent classes using functions like
get_class(), method_exists(), etc.

Serialization:
1. Serialization is the process of converting an object or array into a storable string format
using serialize().
2. It is useful for storing data in files, sessions, or databases and restoring it using
unserialize().
EX:
<?php
class MyClass {
public $name = "Shravan";
public function greet() {}
}

$obj = new MyClass();

// Introspection
echo get_class($obj) . "<br>";

// Serialization
$ser = serialize($obj);
$new = unserialize($ser);

// Output
echo $new->name; // Output: Shravan
?>

OP: MyClass
Shravan
11. List out the database operations.
1.mysqli_affected_rows()
2. mysqli_close()
3. mysqli_connect()
4. mysqli_fetch_array()
5.mysqli_fetch_assoc()
6.mysqli_affected_rows()
7. mysqli_error()
12. Define MySQL.
MySQL is an open-source relational database management system (RDBMS) that stores and
manages data in a structured format using tables. It uses SQL (Structured Query Language)
to perform operations like inserting, updating, deleting, and retrieving data.
ADV:
Open Source ,High Performance, Cross-Platform Support, Security.
13. How traits used in php? Explain with example.
 Since PHP does not support multiple inheritance through classes, Traits are introduced
to allow method reuse across multiple classes.
 Traits help avoid code duplication and allow sharing methods between unrelated classes.
 Declared using the trait keyword and included in a class using use.
 A class can use one or more traits.
 SYNTAX: trait TraitName {
// method definitions
}

class ClassName {
use TraitName;
}
 EX: <?php
trait Greet {
public function sayHi() {
echo "Hi Shravan!";
}
}

class User {
use Greet;
}

$obj = new User();


$obj->sayHi(); // Output: Hi Shravan!
?>

14. Write PHP script a form with 5 checkboxes (ex: programming languages) and a submit
button When selection of one/ more checkboxes, list of selected checkboxes should be
displayed

Html file: <!-- checkbox_form.html -->


<form method="post" action="checkbox_result.php">
<input type="checkbox" name="langs[]" value="PHP"> PHP
<input type="checkbox" name="langs[]" value="Java"> Java
<input type="checkbox" name="langs[]" value="Python"> Python
<input type="checkbox" name="langs[]" value="C++"> C++
<input type="checkbox" name="langs[]" value="JavaScript"> JavaScript
<br><input type="submit" value="Submit">
</form>

Php file:
<?php
// checkbox_result.php
if (!empty($_POST['langs'])) {
echo "Selected languages:<br>";
foreach ($_POST['langs'] as $lang) {
echo $lang . "<br>";
}
} else {
echo "No selection.";
}
?>

Op :

15. Write PHP script to update record in table of MySQL database.


ANS:
<?php
$conn = mysqli_connect("localhost", "root", "", "test");
$sql = "UPDATE users SET name='Updated Name' WHERE id=1";
if (mysqli_query($conn, $sql)) {
echo "Record updated.";
} else {
echo "Error!";
}
mysqli_close($conn);
?>
16. Write PHP script to delete record in table of MySQL database.
ANS:
<?php
$conn = mysqli_connect("localhost", "root", "", "test");
$sql = "DELETE FROM users WHERE id=1";
if (mysqli_query($conn, $sql)) {
echo "Record deleted.";
} else {
echo "Error!";
}
mysqli_close($conn);
?>
17. Write a PHP program to demonstrate session management.
ANS: <?php
session_start();
$_SESSION["user"] = "Shravan";
echo "Session started. User is " . $_SESSION["user"];
?>

18. Create Employee form like employee name, Address, Mobile No., Post and Salary
using different form input element and display user inserted values in new PHP
form.
HTMLFILE:
<!-- employee_form.html -->
<form method="post" action="display_employee.php">
Name: <input type="text" name="name"><br>
Address: <textarea name="address"></textarea><br>
Mobile: <input type="text" name="mobile"><br>
Post: <input type="text" name="post"><br>
Salary: <input type="number" name="salary"><br>
<input type="submit" value="Submit">
</form>
PHP FILE:
<?php
// display_employee.php
echo "Name: " . $_POST["name"] . "<br>";
echo "Address: " . $_POST["address"] . "<br>";
echo "Mobile: " . $_POST["mobile"] . "<br>";
echo "Post: " . $_POST["post"] . "<br>";
echo "Salary: " . $_POST["salary"];
?>

19. Write a program to connect PHP with MySQL.


Ans:
<?php
$conn = mysqli_connect("localhost", "root", "", "test");
if ($conn) {
echo "Connected successfully!";
} else {
echo "Connection failed!";
}
?>
20. Write PHP script to insert record in table of MySQL database.
Ans:
<?php
$conn = mysqli_connect("localhost", "root", "", "test");
$sql = "INSERT INTO users (name, email) VALUES ('Shravan', 'shravan@[Link]')";
if (mysqli_query($conn, $sql)) {
echo "Record inserted.";
} else {
echo "Error!";
}
mysqli_close($conn);
?>
21. Write PHP script to select record in table of MySQL database.
Ans:
<?php
$conn = mysqli_connect("localhost", "root", "", "test");
$result = mysqli_query($conn, "SELECT * FROM users");
while ($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . " - " . $row['email'] . "<br>";
}
mysqli_close($conn);
?>

You might also like