KEMBAR78
Solved ct2 QB WBP | PDF | Http Cookie | Method (Computer Programming)
0% found this document useful (0 votes)
48 views16 pages

Solved ct2 QB WBP

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)
48 views16 pages

Solved ct2 QB WBP

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
You are on page 1/ 16

CT2 QB WBP

1. What is the difference between get and post methods?


ANS:

2. What is the difference between cookie and session


methods.
ANS

3. Define Cookiee. How to create & delete it ? How can we retrieve


a Cookie value?
ANS:

 COOKIE:
i. Cookie is created at server side and saved to client browser.
ii. Each time when client sends request to the server, cookie is embedded with request.
iii. Such way, cookie can be received at the server side. In short, cookie can be created, sent and
received at server end.

 Create a cookie:
i. PHP provides a inbuilt function setcookie(), that can send appropriate HTTP header to create the
cookie on the browser.
ii. While creating the cookie, we have to pass require arguments with it.
iii. Syntax : setcookie(name, value, expire, path, domain, secure, HttpOnly);
 Delete a cookie:
i. Cookie can be deleted from user browser simply by setting expires argument to any past date it
will automatically delete the cookie from user browser. –
ii. Deleted cookie can be checked by calling the same cookie with its name to check if it exists or
not.

 Retrieve cookie:
i. Cookie value can be retrieved using global variable $_COOKIE. –
ii. We use the isset() function to find out if the cookie is set or not.
− Modification of the cookie can simply be done again by setting the cookie using the
setcookie() function.

4. Write a program that demonstrate use of cookies.


Ans:

<html>
<body>
<?php
$cookie_name = "username";
$cookie_value = "abc";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30),
"/"); // 86400 = 1 day
if(!isset($_COOKIE[$cookie_name]))
{
echo "Cookie name '" . $cookie_name . "' is not set!";
}
else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
if(!isset($_COOKIE["user"]))
{
echo "Sorry, cookie is not found!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["user"];
}
setcookie("user"," ",time()-3600);
echo "Cookie 'user' is deleted.";
?>
</body>
</html>

5. Write a PHP program that demonstrate use of session.

Ans: <?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["Name"] = "Vijay";
$_SESSION["Address"] = "Thane";
echo "Session variables are set.";
?>
</body>
</html>
6. How do you connect to a MySQL database using mysqli?
Connecting MySQL Database Server from PHP –
The Process : The process of using MySQL with PHP is as
follows :
1. Connect to MySQL and select the database to use.
2. Build a query string.
3. Perform the query.
4. Retrieve the results and output them to a web page.
5. Repeat steps 2 to 4 until all desired data has been retrieved.
6. Disconnect from MySQL.
Creating a Login File –
When a web site is developed with PHP it contains multiple
program files that will require access to MySQL and will thus
need the login and password details.
- Therefore, it is good to create a single file to store login
credentials and then include that file wherever it is needed.
Example . The login.php file

<?php

$hn = 'localhost';

$db = 'college';

$un = 'root';

$pw = '';

?>

<?php

require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);

if ($conn->connect_error) die($conn->connect_error);
?>
7. Describe the data types of MySQL with its sizes.
Ans:
 String Data type : String Data types allow both fixed and variable
length strings

 Numeric Data Types : Numeric Datatypes allow both signed and


unsigned integers

 Date and Time Data Types : MySQL provides types for date and time as
well as the combination of date and time.
8. Explain any four features of MySQL.
i. Free to download: MySQL is free to use and you can download it from
MySQL official website.
ii. Ease of Management: The software very easily gets downloaded and
also uses an event scheduler to schedule the tasks automatically.
iii. High Performance: MySQL is faster, more reliable and cheaper because
of its unique storage engine architecture.
iv. High Flexibility: MySQL supports a large number of embedded
applications which makes MySQL very flexible.

9. Write the PHP code for fetching the data from a database to
webpage?
Ans:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$databasename = "geeksforgeeks";

// CREATE CONNECTION
$conn = new mysqli($servername,
$username, $password, $databasename);

// GET CONNECTION ERRORS


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

// SQL QUERY
$query = "SELECT * FROM `Student Details`;";

// FETCHING DATA FROM DATABASE


$result = $conn->query($query);

if ($result->num_rows > 0)
{
// OUTPUT DATA OF EACH ROW
while($row = $result->fetch_assoc())
{
echo "Roll No: " .
$row["Roll_No"]. " - Name: " .
$row["Name"]. " | City: " .
$row["City"]. " | Age: " .
$row["Age"]. "<br>";
}
}else {
echo "0 results";
}
$conn->close();
?>

10. Write short note on introspection.

Ans: Introspection:
Introspection in PHP offers the useful ability to examine an object's
characteristics, such as its name, parent
class (if any) properties, classes, interfaces, and methods.

In-built functions in PHP Introspection:

<?php
class Demo
{
// just declare the class as "Demo'
}
if (class_exists('Demo'))
{
$demo = new Demo();
echo "This is Demo class";
}
else
{
echo "Class does not exist";
}
?>
Output :
This is Demo class

11. Explain interface in PHP.

Ans:
i. An Interface allows the users to create programs, specifying the public
methods that a class must implement, without involving the
complexities and details of how the particular methods are
implemented.
ii. An Interface is defined just like a class but with the class keyword
replaced by the interface keyword and just the function prototypes.
iii. The interface contains no data variables.
iv. An interface consists of methods that have no implementations, which
means the interface methods are abstract methods.
v. All the methods in interfaces must have public visibility scope.
vi. Interface enables you to model multiple inheritance.
 Syntax:
Interface (using class along with interface)
class child_class_name extends parent_class_name
implements interface_name1, ..
12. Explain serialization in PHP.
Ans:

 Serializing an object means converting it to a byte stream


representation that can be stored in file.
 Serialize() :
i. The serialize() converts a storable representation of a value.
ii. The serialize() function accepts a single parameter which is the data
we want to serialize and returns a
iii. serialized string.
iv. A serialize data means a sequence of bits so that it can be stored in a
file, a memory buffer or transmitted
v. across a network connection link.
vi. It is useful for storing or passing PHP values around without losing their
type and structure.
 Syntax : serialize(value1);
 Example:
<?php
$s_data= serialize (array ('Welcome', 'to', 'PHP'));
// printing the serialized data
print_r($s_data . "");
$newvar = unserialize ($string);
// printing the unserialized data
print_r($newvar);
?>

13. How traits are used in php with example.


Traits :

Traits define the actual implementation of each method within each class,
traits are just chunks of code injected in a class in PHP.

Traits are not interfaces at all.

Traits can define both static members and static methods.

It helps developers to reuse methods freely in several independent classes in


different class hierarchies.
Traits reduces the complexity, and avoids problems associated with multiple
inheritance.

PHP does not allow multiple inheritance. So Traits is used to fulfil this gap by
allowing us to reuse same functionality in multiple classes.

Syntax :

class child_class_name

use trait_name;

...

child_class functions

Example :

<?php

// Class A i.e. Parent A

class A {

public function disp1()

echo "Parent-A <br>";

// Trait B i.e. Parent B

trait B

public function disp2()


{

echo " Parent-B <br>";

class C extends A

use B;

public function disp3()

echo "\nChild-C";

$obj = new C();

$obj->disp1();

$obj->disp2();

$obj->disp3();
?>

14. Write short note on cloning object.

Object cloning is the process to create a copy of an object.

i. An object copy is created by using the magic method __clone().

ii. If you don’t use the __clone() method, the internal objects of the new
object will be references to the same object in memory as the internal
objects of the original object that was cloned.

iii. An object’s __clone() method cannot be called directly.


iv. When an object is cloned, PHP will perform a shallow copy of all of the
object’s properties.
v. Any properties that are references to other variables will remain
references.

15. Define final class and final method.


Ans:

 final class:

The final class is a class that cannot be extended by other classes. So a Class
that is declared with the final keyword doesn’t have child classes. A
class can be declared as final by prefixing the final keyword before the
Class. The syntax for defining the final class is given below:

Syntax:
final Class className;

 final method:

A method is considered a final method if it is prefixed with the final


keyword. The final methods are the methods that cannot be
overridden. So, these methods can’t be overridden in child/subclasses.
It increases security by preventing the modification of functions. The
syntax for defining a final method is given below:

Syntax:

final function functionName(Parameter1, Parameter2, ...);

16. Explain method overloading concept in PHP with suitable


example.
Ans:
i. In OOP, Method overloading is a feature that allows creating several
methods with a similar name to perform different tasks from one
another depending on the input parameters it accepts as arguments.

ii. It is called static polymorphic i.e method overloading.

iii. In the case of PHP, we have to utilize PHP's magic methods __call() to
achieve method overloading. function __call():

 Syntax:

function __call(string $function_name, array $arguments) { // block of code}

If a class execute __call(), then if an object of that class is called with a


method that doesn't exist then

__call() is called instead of that method.

 Example:

>?php

class Shape {

const PI = 3.142 ;

function __call($name,$arg){

if($name == 'area')

switch(count($arg)){

case 0 : return 0 ;

case 1 : return self::PI * $arg[0] ;

case 2 : return $arg[0] * $arg[1];

}
$circle = new Shape();

echo $circle->area(3);

$rect = new Shape();

echo $rect->area(8,6);

?>

OUTPUT:

9.426

48

In the above example, area () method is created dynamically and executed


with the help of magic method
__call() and its behavior changes according to the passed number of
parameters as object.

List types of inheritance? Explain any one with suitable example .

Inheritance has three types :

Single, multiple and multilevel Inheritance

Single Inheritence:

When a subclass is derived simply from its parent class then this mechanism
is known as simple inheritance.

In case of simple inheritance there is only a sub class and its parent class. It
is also called single inheritance or one level inheritance.

 Syntax :

class Parent

{
// The parent’s class code

Class Child extends Parent

//The child can use the parent’s code

 Example:

<?php

class a

function dis1()

echo "Hello PHP <br>";

class b extends a

function dis2()

echo "PHP Programming <br>";

$obj= new b();

$obj->dis1();

$obj->dis2();
?>

Output :

Hello PHP
PHP Programming

You might also like