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

Test 2 Solution

The document is an examination paper for a Web Based Application Development with PHP course, containing questions on PHP concepts such as constructors, method overloading, object cloning, form methods, and database connections. It includes coding examples and asks students to write PHP programs for various tasks. The test assesses students' understanding of PHP programming and web application development techniques.

Uploaded by

rraut3188
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)
14 views4 pages

Test 2 Solution

The document is an examination paper for a Web Based Application Development with PHP course, containing questions on PHP concepts such as constructors, method overloading, object cloning, form methods, and database connections. It includes coding examples and asks students to write PHP programs for various tasks. The test assesses students' understanding of PHP programming and web application development techniques.

Uploaded by

rraut3188
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

Government Polytechnic, Murtijapur.

Department of Computer Engineering


Second Class Test 2023-2024
Course:- CO6I Subject:- Web Based Application Development with PHP Marks:-20 Time:- 60 min

Q.1. Attempt any Two of the following. (08 Marks)


a) Define constructor and also write the syntax to define constructor in PHP with one
example.
A constructor allows you to initialize an object's properties upon creation of the object. If you
create a __construct() function, PHP will automatically call this function when you create an
object from a class. Notice that the construct function starts with two underscores (__)! We see
in the example below, that using a constructor saves us from calling the set_name() method
which reduces the amount of code:
<?php
class Fruit {
public $name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function get_name() {
return $this->name;
}
function get_color() {
return $this->color;
}}
$apple = new Fruit("Apple", "red");
echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>
b) Write a PHP program to achieve method overloading in PHP?
<?php
class Shape {
function __call($name,$arg){
if($name == 'area')
switch(count($arg)){
case 0 : return 0 ;
case 1 : return 3.142 * $arg[0] * $arg[0] ;
case 2 : return $arg[0] * $arg[1];
}
}
}
$circle = new Shape();
echo $circle->area(3);
$rect = new Shape();
echo $rect->area(8,6);
?>
c) Write PHP program for cloning of an object?
<?php $student1= new Student();
class Student $student1->add= new Address();
{ var $name; $student1->name="Pravin";
var $add; $student1->add->city="Amravati";
function setname($n) $student1->add->pin="444604";
{$this->name=$n; print_r($student1);
} echo "Copy by Assisgnment<BR>";
public function __clone() $student2= clone $student1;
{ $this->add=clone $this->add; $student2->name="Ninad";
}} $student2->add->city="Pune";
class Address $student2->add->pin="444444";
{var $city; echo "After change";
var $pin; print_r($student1);
function setadd($c ,$p) print_r($student2);
{$this->city=$c; ?>
$this->pin=$p;
}}

Q.2. A) Write difference between get() and post() method of form (Any two points) (02 Marks)
get() post()
In GET method we cannot send large amount In POST method large amount of data
of data rather limited data is sent because the
can be sent because the request parameter
request parameter is appended into the URL. is appended into the body
GET request is comparatively better than Post
POST request is comparatively less
so it is used more than the Post request. better than Get so it is used less than the
Get request.
GET request is comparatively less secure POST request is comparatively more
because the data is exposed in the URL bar. secure because the data is not exposed in
the URL bar
Request made through GET method are Request made through POST method is
stored in Browser history. not stored in Browser history.
Request made through GET method are Request made through POST method
stored in cache memory of Browser. are not stored in cache memory of
Browser

Q.2. B) Attempt any One of the following. (04Marks)


a) Write PHP program for web page validation to accept only alphanumeric input only?
<?php
$input = 'Computer123';
if(preg_match('/^[a-zA-Z0-9]+$/', $input))
{
echo 'true';
}
else{
echo 'false';
}
?>
b) Write PHP program for, a form having multiple submit button in one form.
<html>
<title>
Multi Submit Button Working Test
</title>
<body>
<form action="sample.php" method="post">
Username:<br>
<input type="text" name="username"><br>
Password:<br>
<input type="text" name="password"><br><br>
<button type="submit"
formaction="sample1.php"> HOME
</button>
<button type="submit" formaction="sample2.php">Q/A
</button>
</form>
</body>

Q.3. A) How do you connect MYSQL database with PHP write its syntax also (02 Marks)
<?php
$servername = "localhost";
$username = "";
$password = "";
// Create connection Object Oriented
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Q.3. B) Attempt any One of the following. (04Marks)
a) Write PHP program for update operation on table data?
(Any data can be considered for updation)
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "yDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
b) Write PHP program for, select operation on table data.
(Any data can be considered for selecting records)
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "yDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].
"<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

You might also like