0% found this document useful (0 votes)
4 views11 pages

Ad PHP

It's ok
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)
4 views11 pages

Ad PHP

It's ok
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/ 11

Q1  It's a protocol used for exchanging

structured data between


a) Explain the purpose of $this variable.
applications over a network,
 In PHP, $this is a special variable typically using XML and HTTP.
used inside a class.

 It refers to the current object and


d) What is Web Services?
allows access to its properties and
methods.  A Web Service is a software system
designed to support interoperable
Example:
machine-to-machine interaction
php over a network.
CopyEdit  It allows different systems or apps to
communicate via protocols like
class Car {
SOAP or REST.
public $color;

e) List any two PHP HTTP functions.


function setColor($color) {
1. header() – Sends raw HTTP headers.
$this->color = $color; // Refers to the
2. get_headers() – Fetches all headers
current object
sent by the server in response to a
} request.
}

f) What is setcookie() function?


b) Name any two functions to extract basic  The setcookie() function is used to
information about classes in PHP. create a cookie in PHP.
1. get_class() – Returns the class name Example:
of an object.
php
2. get_class_methods() – Returns an
CopyEdit
array of method names in a class.
setcookie("username", "JohnDoe", time() +
3600, "/");
c) What is SOAP?
 This sets a cookie named username
 SOAP stands for Simple Object with value JohnDoe, valid for 1 hour.
Access Protocol.
2. Dynamic content loading without
page refresh (e.g., loading
g) Enlist the PHP DOM functions.
comments on scroll).
1. DOMDocument::load() – Loads an
XML document.
Q2
2. DOMDocument::getElementsByTagN
ame() – Returns a list of elements a) What is Introspection? Explain
with a given tag name. any two introspective functions.

3. DOMDocument::createElement() –  Introspection in PHP refers to the


Creates a new element node. ability to examine the properties,
methods, and other aspects of a
class at runtime.
h) What is XML Parser?
 It allows you to retrieve metadata
 An XML Parser reads XML data and about a class, such as its methods,
converts it into a format that properties, and constants, without
programs can use. having direct access to the source
code.
 In PHP, you can parse XML using:
Two introspective functions:
o SAX Parser (event-based)
1. get_class()
o DOM Parser (tree-based)
o Purpose: Returns the name
of the class of an object.
i) Which are the parts of XML-RPC?
o Example:
1. XML Format – For encoding data.
php
2. HTTP Protocol – For transport.
CopyEdit
3. Remote Procedure Calls (RPC) – To
class Car {
call functions on remote systems.
public $color = "red";

}
j) Give any two applications of AJAX.

1. Live search suggestions (e.g., Google


auto-suggest). $car = new Car();

echo get_class($car); // Output: Car

2. method_exists()
o Purpose: Checks if a method Name: <input type="text"
exists in a class. name="name" value="<?php echo
$_POST['name'] ?? ''; ?>">
o Example:
Age: <input type="text"
php
name="age" value="<?php echo
CopyEdit $_POST['age'] ?? ''; ?>">

class Car { <input type="submit">

public function drive() { </form>

echo "Driving";

} <?php
} if ($_SERVER["REQUEST_METHOD"]
== "POST") {

echo "Name: " . $_POST['name'] .


$car = new Car();
"<br>Age: " . $_POST['age'];
echo method_exists($car, 'drive'); //
}
Output: 1 (true)
?>

 When the form is submitted, the


b) What is a sticky form? Explain
values entered in the form will
with an example.
remain in the input fields.
 A sticky form is a form that retains
user input after a form is submitted,
so the user doesn't need to re-enter c) Explain how to create and select
the data if the form submission fails. a database using PHP.

 It is achieved by pre-filling the form To create and select a database in


with the previously entered values PHP, you can use the mysqli or PDO
using the $_POST variable in PHP. extension.

Example of a sticky form: Example using mysqli:

php php

CopyEdit CopyEdit

<form method="post"> // Create a connection to MySQL


server
$conn = mysqli_connect("localhost",  AJAX (Asynchronous JavaScript and
"root", ""); XML) allows a web page to request
data from the server
asynchronously, meaning the page
// Check the connection doesn't need to reload to update
content.
if (!$conn) {
 The server processes the request
die("Connection failed: " .
and returns data (often in JSON or
mysqli_connect_error());
XML format) to the client-side,
} which then updates the page
dynamically.

AJAX Web Application Model:


// Create a database
1. Client-Side: The JavaScript code on
$sql = "CREATE DATABASE myDB";
the client (browser) sends an
if (mysqli_query($conn, $sql)) { asynchronous request to the server
echo "Database created without refreshing the page.
successfully"; 2. Server-Side: The server receives the
} else { request, processes it, and sends
back data (often as a response).
echo "Error creating database: " .
mysqli_error($conn); 3. JavaScript: JavaScript processes the
server's response and updates the
} webpage dynamically.

Example:
// Select the database html
mysqli_select_db($conn, "myDB"); CopyEdit
 This script first connects to the <!-- HTML Form -->
MySQL server, creates a database
myDB, and then selects it for further <input id="name">
operations. <button
onclick="sendData()">Submit</butt
on>
d) Explain the AJAX web application
model.
<!-- Placeholder for response -->
<div id="result"></div> 2. Use the $_FILES array to access the
uploaded file.

3. Move the uploaded file from its


<script>
temporary location to the desired
function sendData() { folder using move_uploaded_file().

let name = Example:


document.getElementById('name').v
html
alue;
CopyEdit
fetch('server.php?name=' + name)
<!-- HTML Form for File Upload -->
.then(response =>
response.text()) <form action="upload.php"
method="post"
.then(data =>
enctype="multipart/form-data">
document.getElementById('result').i
nnerHTML = data); Select file: <input type="file"
name="fileToUpload">
}
<input type="submit"
</script>
value="Upload File"
In this example, when the user clicks name="submit">
Submit, the JavaScript function
</form>
sends the request to the server
(server.php), and the server's PHP Script (upload.php):
response is displayed in the <div>
php
without refreshing the page.
CopyEdit

if (isset($_POST['submit'])) {
e) How to handle file upload in
PHP? $target_dir = "uploads/";

 In PHP, you can use the $_FILES $target_file = $target_dir .


superglobal to handle file uploads. basename($_FILES["fileToUpload"]
["name"]);
Steps to handle file upload:

1. Create an HTML form with the


enctype="multipart/form-data" if
attribute. (move_uploaded_file($_FILES["fileTo
Upload"]["tmp_name"], php
$target_file)) {
CopyEdit
echo "The file " .
// Class definition
basename($_FILES["fileToUpload"]
["name"]) . " has been uploaded."; class Car {

} else { public $color;

echo "Sorry, there was an error public $model;


uploading your file.";

}
function setDetails($color,
} $model) {

Explanation: $this->color = $color;

 The form allows users to select a file $this->model = $model;


for upload.
}
 The PHP script checks if the form has
been submitted and moves the
uploaded file to the uploads/ function display() {
directory using
echo "Car Model: " . $this-
move_uploaded_file().
>model . ", Color: " . $this->color;
Q4) Attempt any FOUR of the
}
following:
}
a) Define class and object. Explain
with example.

 Class: A class is a blueprint or // Object creation


template for creating objects. It $car1 = new Car();
defines properties (attributes) and
methods (functions) that an object $car1->setDetails("Red", "Toyota");
created from the class can have. $car1->display();
 Object: An object is an instance of a  In this example:
class. It is created based on the class
definition and can access its o Class Car is defined with
properties and methods. properties like color and
model.
Example:
o Object $car1 is created from // Access session variables
the Car class and has its
echo 'Hello, ' .
properties and methods
$_SESSION['username']; // Output:
accessed.
Hello, JohnDoe

b) What is PHP session? Explain to


// Destroy the session
start and how to destroy PHP
session with example. session_destroy();

 A PHP session is a way to store user  session_start() initializes the


information across multiple pages. session.
Sessions allow data to be retained
 $_SESSION['username'] stores data
on the server for the duration of the
that can be accessed across pages.
user's visit, unlike cookies which
store data on the client side.  session_destroy() ends the session
and clears stored data.
Start a Session:

 Use the session_start() function at


the beginning of your PHP script to c) Write a PHP script to create
initialize the session. 'product.xml', which contains p_no,
p_name, color, weight. Add four
Destroy a Session:
elements into it. Write PHP script to
 You can use session_destroy() to display product.xml in table format.
completely destroy the session.
PHP script to create and add
Example: elements to 'product.xml':

php php

CopyEdit CopyEdit

// Start the session // Create a new SimpleXMLElement


object
session_start();
$xml = new
SimpleXMLElement('<products></pr
// Store session variables oducts>');

$_SESSION['username'] = 'JohnDoe';

// Add product data


$product1 = $xml- echo "<table border='1'>
>addChild('product');
<tr>
$product1->addChild('p_no', '101');
<th>Product No</th>
$product1->addChild('p_name',
<th>Product Name</th>
'Laptop');
<th>Color</th>
$product1->addChild('color', 'Black');
<th>Weight</th>
$product1->addChild('weight', '2.5
kg'); </tr>";

$product2 = $xml- // Loop through each product and


>addChild('product'); display in table rows

$product2->addChild('p_no', '102'); foreach ($xml->product as $product)


{
$product2->addChild('p_name',
'Smartphone'); echo "<tr>

$product2->addChild('color', 'Blue'); <td>" . $product->p_no .


"</td>
$product2->addChild('weight', '0.5
kg'); <td>" . $product->p_name .
"</td>

<td>" . $product->color .
// Save XML to file
"</td>
$xml->asXML('product.xml');
<td>" . $product->weight .
PHP script to display 'product.xml' "</td>
in table format:
</tr>";
php
}
CopyEdit

// Load the XML file


// End the table
$xml =
echo "</table>";
simplexml_load_file('product.xml');

// Start the table


d) Write a PHP program which }
implements AJAX for addition of
</script>
two numbers.
</head>
HTML Form and JavaScript (AJAX):
<body>
html

CopyEdit
<input type="text" id="num1"
<!DOCTYPE html>
placeholder="Enter number 1">
<html>
<input type="text" id="num2"
<head> placeholder="Enter number 2">

<title>AJAX Addition</title> <button


onclick="addNumbers()">Add</butt
<script>
on>
function addNumbers() {
<div id="result"></div>
var num1 =
document.getElementById('num1').v
alue; </body>

var num2 = </html>


document.getElementById('num2').v
PHP script (add.php):
alue;
php
var xhr = new XMLHttpRequest();
CopyEdit
xhr.open('GET', 'add.php?num1='
+ num1 + '&num2=' + num2, true); <?php

xhr.onload = function() { if (isset($_GET['num1']) &&


isset($_GET['num2'])) {
if (xhr.status == 200) {
$num1 = $_GET['num1'];

document.getElementById('result').i $num2 = $_GET['num2'];


nnerHTML = xhr.responseText;
echo $num1 + $num2; // Output
} the sum

} }

xhr.send(); ?>
Featu
GET POST
re
e) Differentiate between GET and
POST method. e).
Featu For
GET POST
re For sendi
retrievin ng
Used
g data or sensi
to Usag
non- tive
send e
Used to sensitive data
data
Purpo send requests or
in the
se data in . larger
body
the URL. data.
of the
reque
st.
Q5) Write a short note on any Two
Data of the following:
is
a) Super Global Variables:
hidde
Data is n in  Super Global Variables are built-in
Visibi
visible in the global arrays in PHP that can be
lity
the URL. body accessed from any part of the script,
of the regardless of scope.
reque
 They are used to handle data coming
st.
from the user or server.
Limited
No Common Super Global Variables:
Data (by URL
practi
Lengt length 1. $_GET – Collects form data sent via
cal
h restrictio the GET method.
limit.
ns). 2. $_POST – Collects form data sent via
Secur Less More the POST method.
ity secure secur 3. $_SESSION – Used to store session
(data is e variables.
visible). (data
is not 4. $_FILES – Contains file upload
visibl information.

5. $_COOKIE – Used to handle cookies.


b) AJAX:

 AJAX (Asynchronous JavaScript and


XML) allows the web page to
request data from the server
asynchronously, meaning the page
does not reload when requesting or
receiving data.

 It enhances the user experience by


enabling dynamic content updates
without refreshing the entire page
(e.g., live search, form validation).

c) Content Management System


(CMS):

 A Content Management System


(CMS) is a software application used
to manage digital content.

 It allows users to create, edit, and


organize content without needing
extensive technical knowledge.

Popular CMS examples: WordPress,


Joomla, Drupal.

 A CMS typically offers an Admin


Panel where users can manage
pages, media, and content in a user-
friendly interface

You might also like