1.
Demonstrate the creation of classes in PHP with example:
```php
class Car {
public $color;
public $model;
public function construct($color, $model) {
$this->color = $color;
$this->model = $model;
public function message() {
return "My car is a " . $this->color . " " . $this->model . ".";
$myCar = new Car("black", "Volvo");
echo $myCar->message();
```
2. Explain cloning object:
Cloning in PHP creates a copy of an object using the `clone` keyword. The ` clone()` method can
be used to customize the cloning process.
<?php
class Person {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
$person1 = new Person("Alice");
$person2 = clone $person1;
echo $person1->name; // Output: Alice
echo "<br>";
echo $person2->name; // Output: Alice (Clone)
?>
3. List any 2 functions used in Serialization:
- `serialize()`
- `unserialize()`
-
<?php
$user = ["name" => "John", "email" => "john@[Link]"];
// Serialize
$serialized = serialize($user);
echo $serialized;
// Unserialize
$unserialized = unserialize($serialized);
print_r($unserialized);
?>
4. Define MySQL:
5. MySQL is an open-source relational database management system (RDBMS) that is used to
store, manage, and retrieve data efficiently. It uses Structured Query Language (SQL) for
accessing and manipulating databases. MySQL is widely used in web development and supports
multi-user access to databases. It is known for its speed, reliability, and ease of use. MySQL is
often used in combination with PHP and Apache in a software stack like LAMP (Linux, Apache,
MySQL, PHP) to build dynamic websites and web applications.
6. List any 4 functions used in connectivity of database:
- `mysqli_connect()`
- `mysqli_query()`
- `mysqli_close()`
- `mysqli_num_rows()`
- `mysqli_fetch_assoc()`
7. Elaborate the following:
1)`mysqli_query()`: Executes a query on the database.
2) `mysqli_connect()`: Establishes a connection to a MySQL database.
8. Compare constructor and destructor (any 4 points):
- Constructor is called when an object is created; Destructor is called when the object is destroyed.
- Constructor initializes properties; Destructor cleans up resources.
- Constructor can accept arguments; Destructor cannot.
- Constructor is defined with ` construct()`; Destructor with ` destruct()`.
9. Compare session and cookies (4 points):
- Sessions are stored on the server; Cookies on the client.
- Sessions are more secure; Cookies are less secure.
- Sessions expire when browser closes; Cookies can persist.
- Sessions can store large data; Cookies have size limits.
10. Demonstrate the concept of overloading:
PHP does not support traditional function overloading, but property overloading can be done using
magic methods like `__call()`, ` callStatic()`.
<?php
class over{
public function __call($meth,$args){
echo "methode:".$meth."addition".array_sum($args);
}
}
$obj=new over();
$obj->add(10,20,30);
?>
11. Demonstrate the concept of overriding:
<?php
class ParentClass
{ public function show()
echo "Parent class";
class ChildClass extends ParentClass
{ public function show() {
echo "Child class";
$obj = new ChildClass();
$obj->show();
?>
12. Illustrate the webpage validation with example:
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
if(empty($_POST['name'])){
echo "enter a name";
} else{
echo $_POST['name'];
}
if(empty($_POST['phoneno'])){
echo "enter a phoneno";
} else{
echo $_POST['phoneno'];
}
if(empty($_POST['email'])){
echo "enter a email";
} else{
echo $_POST['email'];
}
}
?>
<html>
<body>
<form action="[Link]" method="post">
Enter the name:
<input type="text" name="name">
Enter the email:
<input type="text" name="email">
Enter the phone no:
<input type="text" name="phoneno">
<input type="submit" name="submit">
</form>
</body>
</html>
13. Create customer form:
-><?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
echo $_POST['name'];
echo $_POST['address'];
echo $_POST['mobileno'];
echo $_POST['dob'];
}
?><html>
<body>
<form action="[Link]" method="post">
Enter the name:<input type="text" name="name">
Enter the address:
<input type="text" name="address">
Enter the mobile no:
<input type="text" name="mobileno">
Enter the date of birth:
<input type="text" name="dob">
<input type="submit" name="submit"></form>
</body>
</html>
[Link] PHP code of update & delete operation:
-><?php
$user="root";
$server="localhost";
$password="";
$dbname="user";
$conn=mysqli_connect($server,$user,$password,$dbname);
$sql="UPDATE `userinfo` WHERE `userid` like '15'";
mysqli_query($conn,$sql);
$sql="DELETE `userinfo` WHERE `userid` like '20'";
mysqli_query($conn,$sql);
?>
14. Explain session & cookies:
<?php
//set cookie
setcookie("user","bob",time()+3600,"/");
//get cookie
if(isset($_COOKIE["user"])){
echo $_COOKIE["user"];
}
//set session
session_start();
$_SESSION["user"]="sahil";
if(isset($_SESSION["user"])){
echo $_SESSION["user"];
}
?>
- **Session**: Server-side storage, lasts until the browser is closed or session expires.
- **Cookie**: Client-side storage, can persist based on expiry date set.
- Both are used for maintaining user state across web pages.