Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Couse Title: Web based Application development using PHP (22619)

Q1. Attempt any FIVE of the following.


a) List any Four Advantages of PHP
1. PHP is easy to use.
2. PHP is free and open source.
3. PHP is flexible language.
4. PHP supports many databases connectivity’s such as MySQL, Oracle etc.,
5. PHP is platform independent.
6. PHP is loosely typed language.

b) State the use of str_word_count along with its syntax.


The str_word_count() is in-built function is PHP which is used to count the number of
words in a string.
Syntax: str_word_count(string)
Example:
<?php
echo str_word_count("Welcome to PHP!");
?>
Output: 3

c) Define Serialization.
It is a technique used by programmers to preserve there working data in specific format
that can later be restore to its previous format. Serializing an object means converting
it to a byte stream representation that can be stored into a file for that serialize() method
is used.
Syntax: serialize(value)

d) Write the syntax for creating cookies.


Syntax: setcookie(name, value, expire, path, domain, secure)

e) Write the syntax of connecting PHP webpage with MySQL.


Syntax: mysqli_connect($server, $username, $password, $database_name)

f) Define GET and POST methods.


GET method: The GET method sends the encoded user information appended to the
page request (to the URL). The code and the encoded information are separated by the
? character. Can be bookmarked and cached. GET has limits on the amount of
information to send. The limitation is about 2048 characters.

POST method: Information sent from a form with the post method is invisible to others
(all names/values are embedded within the body of the HTTP request). Cannot be
bookmarked and cached. POST has no limits on the amount of information to send.
g) State the use of “$” sign in PHP
The “$” sign is used for declaring a variable in PHP.
Example: $name= "Amrutvahini"

Q2. Attempt any THREE of the following.


a) Write program using foreach loop.
Program:
<?php
$arr=array(1,2,3,4,5,6);
foreach ($arr as $i) {
echo $i;
echo "<br>";
}
?>
Output:
1
2
3
4
5
6

b) Explain Indexed and Associative arrays with suitable example.


1) Indexed Array: An indexed array has a numeric index value and they are accessed
and stored in linear fashion. These arrays can store numbers, Strings and any object but
their index will be represented by numbers by default, the array index starts from 0.
Syntax:
$arrayName = array(element 1,element 2…..element n );
Program:
<?php
$arr=array(1,"PHP",3,4,"Python",6);
foreach ($arr as $i) {
echo $i;
echo "<br>";
}
?>
Output:
1
PHP
3
4
Python
6
2)Associative array: An Associative array has index as string. This stores element
values in association with key:values. Associative array will have their index as string
so that you can establish a strong association between key and values.
Syntax:
$arrayName = array('key1' => 'value1', 'key2' => 'value2'…..'key n' => 'value n' );
Program:
<?php
$arr = array('ID' => 1, "Name"=>"Amrut" );
foreach ($arr as $key => $i) {
echo $key." ".$i;
echo "<br>";
}
?>
Output:
ID1
Name Amrut

c) Define Introspection and explain it with suitable example.


Introspection: PHP offers useful ability to examine an object characteristic such as its
name, parent name, classes, interface & methods.
Program:
<?php
class demo
{

}
class demo1 extends demo
{
function display()
{

}
}
if(class_exists("demo")){
echo "Demo Class exists <br>";
}
else {
echo "Class does not exists";
}
$obj=new demo1();
echo "Class Name:";
echo get_class($obj);
echo "<br> Parent Class Name: ";
echo get_parent_class($obj);
echo "<br> Methods: ";
$arr=get_class_methods("demo1");
foreach ($arr as $i) {
print $i;
}
?>
Output:
Demo Class exists
Class Name: demo1
Parent Class Name: demo
Methods: display

d) Differentiate between session and cookies.


Sr. Session Cookies
No.
1. A session is used to save information A cookie is a small text file that is
on the server saved on the user’s computer.
2. The session is server-side resource. The cookie is client-side resource.
3. It stored unlimited amount of data It stored limited amount of data
4. It is holding multiple variables in It is not holding multiple variables in
sessions. cookies.
5. We cannot access the session values We can access the cookie values
easily. So, it is more secure. easily. So, it is less secure.
6. Contains more complex information. Contains an id string

Q3. Attempt any THREE of the following.


a) Differentiate between implode and explode functions.

Sr. implode() explode()


No.
1. implode() is in-built function in explode() is in-built function in PHP
PHP which is used to join the which breaks a string into an array.
element of an array.
2. It returns the string from the It returns an array of string
elements of an array.
3. Syntax: Syntax:
implode(separator, array) explode(separator, string)
4. Example: Example:
<?php <?php
$course[0]="CM"; $str="Hello,php";
$course[1]="IT"; $a=explode(",", $str);
$a=implode("/",$course ); print_r($a)
Echo $a; ?>
?> Output: Array ( [0] => Hello [1] =>
Output: CM/IT php )
b) Write a program for Cloning of an object.
Program:
<?php
class sample{
public $v1;
public $v2;
function __construct($v1,$v2){
$this->v1=$v1;
$this->v2=$v2;
}
}
$s1=new sample("Amrut", "vahini");
$s2=$s1;
$s1->v2="poly";
print_r($s1);
echo "<br>";
print_r($s2)
?>
Output:
sample Object ( [v1] => Amrut [v2] => poly )
sample Object ( [v1] => Amrut [v2] => poly )

c) Define session and explain how it works.


Session: A session is a way to store information (in variables) to be used across multiple
pages. Session allows us to store data on web server that associated with a session ID.
When you work with an application, you open it, do some changes, and then you close
it. This is much like a Session.
Working:
The computer knows who you are. It knows when you start the application and when
you end. But on the internet, there is one problem: the web server does not know who
you are or what you do, because the HTTP address doesn't maintain state.
Session variables solve this problem by storing user information to be used across
multiple pages (e.g., username, favourite colour, etc). By default, session variables last
until the user closes the browser.
So; Session variables hold information about one single user, and are available to all
pages in one application.

d) Write update and delete operations on table data.


Update: Data can be updated into MySQL tables by executing SQL UPDATE
statement through PHP function mysql_query().
Program:
<?php
$host="localhost";
$user="root";
$pass="";
$dbname="test";
$conn=mysqli_connect($host,$user,$pass,$dbname);
if(!$conn){
die("Could not connect:".mysqli_connect_error());
}
echo "Connected Successfully";
echo "<br>";
$sql="update sample set name=\"Rajesh\", Salary=37000 where ID=2";
if (mysqli_query($conn,$sql)) {
echo "Record Updated Successfully";
}
else{
echo "Could not update record";
}
mysqli_close($conn);
?>
Output:
Connected Successfully
Record Updated Successfully

Delete: Data can be deleted into MySQL tables by executing SQL DELETE statement
through PHP function mysql_query().
Program:
<?php
$host="localhost:3307";
$user="root";
$pass="";
$dbname="test";
$conn=mysqli_connect($host,$user,$pass,$dbname);
if(!$conn){
die("Could not connect:".mysqli_connect_error());
}
echo "Connected Successfully";
echo "<br>";
$sql="delete from sample where id=1";
if(mysqli_query($conn,$sql)){
echo "Deleted Successfully";
}
else{
echo "Could not delete the record";
}
mysqli_close($conn);
?>
Output:
Connected Successfully
Deleted Successfully
Q4. Attempt any THREE of the following.
a) State the variable function. Explain it with example.
Variable function: PHP supports the concept of Variable function. This means that if a
variable name has parenthesis appended to it. PHP will look for a function with the
same name as whatever the variable evaluated to, it and will attempt to execute it.
Program:
<?php
function display()
{
echo "In function display()";
echo "<br>";
}
function sample(){
echo "In function sample()";
}
$a="display";
$a();
$b="sample";
$b();
?>
Output:
In function display()
In function sample()

b) Explain the concept of serialization with example.


It is a technique used by programmers to preserve there working data in specific format
that can later be restore to its previous format. Serializing an object means converting
it to a byte stream representation that can be stored into a file for that serialize() method
is used.
Syntax: serialize(value)
Program:
<?php
$str=serialize(array("Welcome", "to","PHP"));
print_r($str);
?>

c) Answer the following


I. Get session Variables:
Session variables are not passed individually to each new page, instead they are
retrieved from the session we open at the beginning of each page. All session variable
values are stored in the global $_SESSION variable.
Program
<?php
session_start();
$_SESSION["favcolor"]="green";
echo "Your favourite colour is ".$_SESSION["favcolor"];
?>
Output:
Your favourite colour is green

II. Destroy session


To remove all global variables and destroy the session, session_destroy() function is
used. It does not take any arguments and a single call can destroy the session.
Program:
<?php
session_start();
$_SESSION["favcolor"]="green";
echo "Your favourite colour is ".$_SESSION["favcolor"];
session_destroy();
?>

d) Explain inserting and retrieving the query result operations.


Insert data: Data can be updated into MySQL tables by executing SQL UPDATE
statement through PHP function mysql_query().
Program:
<?php
$host="localhost";
$user="root";
$pass="";
$db="test";
$conn=mysqli_connect($host,$user,$pass,$db);
if(!$conn){
die("Cannot connect the database");
}
else{
echo "Database connected successfully";
}
$sql='insert into sample(id,name,salary) values(3,"Suraj",40000)';
if(mysqli_query($conn,$sql)){
echo "<br>Record Inserted";
}
else{
echo "Could not insert the record";
}
mysqli_close($conn);
?>Output:
Database connected successfully
Record Inserted

Retrieve the query result: Data can be retrieved into MySQL tables by executing SQL
SELECT statement through PHP function mysql_query().

<?php
$host="localhost:3307";
$user="root";
$pass="";
$db="test";
$conn=mysqli_connect($host,$user,$pass,$db);
if(!$conn){
die("Database cannot connect");
}
else{
echo "Database connected<br>";
}
$sql='select * from sample';
$val=mysqli_query($conn,$sql);
if(mysqli_num_rows($val)>0){
while($row=mysqli_fetch_assoc($val)) {
echo "ID: {$row['ID']}<br>";
echo "Name: {$row['Name']}<br>";
echo "Salary: {$row['Salary']}<br>";
}}
else{
echo "No results";
}
mysqli_close($conn);
?>
Output:
Database connected
ID: 2
Name: Rajesh
Salary: 37000
ID: 3
Name: Suraj
Salary: 40000
e) Create a web page using GUI components.
<!DOCTYPE html>
<html>
<body>
<form name="f1" method="post" action="">
Enter Your Name here:<input type="text" name="name">
<br><br>
Enter Your Address Here:<textarea name="txtarea"></textarea>
<br><br>
Gender:
<input type="radio" name="rb">Male
<input type="radio" name="rb">Female
<br><br>
Hobbies:
<input type="checkbox" name="check"> Dancing
<input type="checkbox" name="check"> Playing
<input type="checkbox" name="check"> Singing
<br><br>
<input type="submit" name="Submit ">
</form>
</body>
</html>

Q5. Attempt any TWO of the following.


a) Explain any three data types used in PHP.
Data types are used to hold different types of data or value. PHP supports 8 primitive
data type that can be divide into 3 types.
1. Scalar Type:
i. Integer: This data type only holds numeric value. Hols only whole number
including positive any negative numbers.
ii. Float: This data type holds floating point numbers or represents numeric value
with decimal points
iii. String: A string is a sequence of characters. A string is declared using single
quote or double quote.
iv. Boolean: Boolean data type represents Boolean true (1) or false (0).
2. Compound data type:
i. Array: An array stores multiple value in a single variable and each value is
identified by position.
ii. Object: Object are defined as instance of user defined class that can be hold both
value and functions.
3. Special data type:
i. Resource: The Resource data type is not actual data type. It stores reference to
function.
ii. Null: Null is a special data type which can have only one value NULL.
b) Write a program to connect PHP with MySQL.
Program:
<?php
$host="localhost";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass);
if(!$conn){
die("Could Not Connect database");
}
else{
echo "Database Connected";
}
mysqli_close($conn);
?>
Output:
Database Connected

c) Explain the concept of overriding in detail.


The purpose of overriding is to change the behaviour of parent class method. The two
methods with same name and same parameter is called overriding. In function/method
overriding both parent and child classes should have same function name with same
number of arguments.
Program:
<?php
class p
{
function dis1()
{
echo "This is parent class<br>";
}
}
class child extends p
{
function dis1()
{
echo "This is child class";
}}
$p=new p();
$p->dis1();
$c=new child();
$c->dis1();
?>
Output:
This is parent class
This is child class
Q6. Attempt any TWO of the following.
a) Explain web page validation with example.
Validation means check the input submitted by the user. There are two types of
validation are available in PHP.
1. Client-side validation: Validation is performed on the client machine web browsers.
2. Server-Side Validation: After submitted by data, The data has sent to a server and
perform validation checks in server machine.
Program:
<html>
<body>
<?php
$nameErr = $emailErr = $genderErr = $addrErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}
if (empty($_POST["addr"])) {
$addrErr="Address is required";
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
}
}
?>
<form name="f1" method="post" action="">
Enter Your Name here:<input type="text" name="name">
<?php echo $nameErr;?>
<br><br>
Enter Your Address Here:<textarea name="addr"></textarea>
<?php echo $addrErr;?>
<br><br>
Enter Your E-mail here:<input type="email" name="email">
<?php echo $emailErr;?>
<br><br>
Gender:
<input type="radio" name="gender">Male
<input type="radio" name="gender">Female
<?php echo $genderErr;?>
<br><br>
<input type="submit" name="Submit ">
</form>
</body></html>
b) Write a program to create PDF document in PHP.
<?php
require('fpdp.php');
$pdf= new FPDP();
$pdf->AddPage();
$pdf->setFont('Arial','B',16);
$pdf->cell(60,10,"Amrutvahini Polytechnic",1,1,'c');
$pdf->Output();
?>
c) Elaborate the following:
I. __call():
The __call() function is triggered while invoking overloading methods in the
object context.
Syntax:
__call($method, $parameters)
Program:
<?php
class addition
{
function __call($name,$arguments)
{
if($name=="add")
{
switch (count($arguments)) {
case 1:
return $arguments[0];
break;
case 2:
return $arguments[0]+$arguments[1];
break;
case 3:
return $arguments[0]+$arguments[1]+$arguments[2];
break;
}
}
}
}
$s=new addition;
echo "One value".$s->add(10)."<br>";
echo "Addition of two numbers".$s->add(10,30)."<br>";
echo "Addition of two numbers".$s->add(10,30,50)."<br>";
?>
Output:
One value 10
Addition of two numbers 40
Addition of two numbers 90

II. mysqli_connect():
The mysqli_connect() function is used to connect with MySQL database. It returns
resource if connection is established or not.
Syntax:
mysqli_connect(server, username, password, database_name)
Program:
<?php
$host="localhost";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass);
if(!$conn){
die("Could Not Connect database");
}
else{
echo "Database Connected";
}
mysqli_close($conn);
?>
Output:
Database Connected

You might also like