0% found this document useful (0 votes)
16 views

Student Copy Slips With Program Solution

The document contains a series of PHP practical exercises for a course on Advanced PHP, covering topics such as creating a calculator class, demonstrating introspection, defining classes and subclasses, and implementing interfaces. Each slip includes specific tasks, example code, and expected outputs related to object-oriented programming concepts in PHP. The exercises also involve user input through forms and display of results based on the operations performed.

Uploaded by

Rani Suryvanshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Student Copy Slips With Program Solution

The document contains a series of PHP practical exercises for a course on Advanced PHP, covering topics such as creating a calculator class, demonstrating introspection, defining classes and subclasses, and implementing interfaces. Each slip includes specific tasks, example code, and expected outputs related to object-oriented programming concepts in PHP. The exercises also involve user input through forms and display of results based on the operations performed.

Uploaded by

Rani Suryvanshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Advance PHP Sem-IV

S.Y.BBA(C.A) Practical slips


Slip - 1

Slip1: Write a PHP script to create a simple calculator that can


accept two numbers and perform operations like add, subtract,
multiplication. (Use the concept of Class)
[Marks 15]
<?php
class Calculator
{
public $a,$b;
function __construct($a1,$b1)
{
$this->a = $a1;
$this->b = $b1;
}
function add()
{
return $this->a + $this->b;
}
function sub()
{
return $this->a - $this->b;
}
function mul()
{
return $this->a * $this->b;
}
function div()
{
return $this->a / $this->b;
}
}
$calc = new Calculator(3,4);
echo "Addition is : " .$calc->add()."<br>";
echo "substraction is : " .$calc->sub()."<br>";
echo "Multiplication is : " .$calc->mul()."<br>";
echo "division is : " .$calc->div()."<br>";
?>
Slip2 :Write a PHP script to demonstrate the introspection for examining
classes and objects. (use function get_declared_classes( ) ,get_class_methods(
) and get_class_vars( )

<?php
class demo
{
public $a,$b;
function show()
{
//echo "hi";
}

function sample()
{
}
}
$obj= new demo();
$obj->show();
print_r(get_class_methods("demo"));
print_r(get_declared_classes());
print_r(get_class_vars("demo"));
?>

Slip 3: Write a calculator class that can accept two values, then add, subtract,
multiply them or divide them on request.
For example:
$calc = new Calculator( 3,
4 ); echo $calc- >add(); II
Displays "7"
echo $calc- >multiply(); II Displays "12"
Calform.html
<!doctype html>
<html>
<head>
</head>
<body>
<form action="calculator.php" method=get>
Enter 1st value :
<input type="text" name=a> <br><br><br>
Enter 2nd value :
<input type="text" name=b> <br><br><br>
<input type = "submit">
</form>
</body>
</html>

Calculator.php
<?php
class Calculate
{
public $a;
public $b;
function __construct($a,$b)
{
$this->a=$a;
$this->b=$b;
}
public function add()
{
$c=$this->a+$this->b;
echo"Addition = $c<br>";
}
public function subtract()
{
$c=$this->a-$this->b;
echo"Subtract = $c<br>";
}
public function multiply()
{
$c=$this->a*$this->b;
echo"Multiplication = $c<br>";
}
public function div()

{
$c=$this->a/$this->b;
echo"Division = $c";
}
}
$x=$_GET['a'];
$y=$_GET['b'];
$calc=new Calculate($x,$y);
$calc->add();
$calc->subtract();
$calc->multiply();
$calc->div();
?>

Slip 4: Define a class Employee having private members - id, name, department,
salary. Define parameterized constructors. Create a subclass called "Manager"
with private member bonus. Create 3 objects of the Manager class and display the
details of the manager having the maximum total salary (salary+ bonus).
<?php
class Employee
{
private $eid,$ename,$edept,$sal;
function __construct($a,$b,$c,$d)
{
$this->eid=$a;
$this->ename=$b;
$this->edept=$c;
$this->sal=$d;
}
public function getdata()
{
return $this->sal;
}
public function display()
{
echo "employee id : ".$this->eid."</br>";
echo "employee name : ".$this->ename."</br>";
echo "employee department : ".$this->edept."</br>";
}
}

class Manager extends Employee


{
private $bonus;
public static $total1=0;
function __construct($a,$b,$c,$d,$e)
{
parent::__construct($a,$b,$c,$d);
$this->bonus=$e;
}
public function max($ob)
{
$sal=$this->getdata();
$total=$sal+$this->bonus;
if($total>self::$total1)
{
self::$total1=$total;
return $this;
}
else
{
return $ob;
}
}
public function display()

{
parent::display();
echo "maximum salary is : ".self::$total1;
}
}
$ob=new Manager(0,"ABC","",0,0);
$ob1=new Manager(1,"Akansha","hr",28000,2000);
$ob=$ob1->max($ob);
$ob2=new Manager(2,"Ramesh","Acct",30000,2500);
$ob=$ob2->max($ob);
$ob3=new Manager(3,"Riya","Tester",32000,1000);
$ob=$ob3->max($ob);
$ob->display();
?>

Slip5:
Create an abstract class Shape with methods area( ) and volume( ). Derive
three classes rectangle (length, breath), Circle(radius) and Cylinder(radius,
height), Calculate area and volume of all. (Use Method overriding).

<?php

define('pi',3.14);
define('four_thirds',4/3);

abstract class Shape


{
private $x = 0;
private $y = 0;
public abstract function area();
public abstract function volume();
}

/*class Rectangle extends Shape


{
function __construct($x, $y)
{
$this->x = $x;
$this->y = $y;
}
function area()
{
return $this->x * $this->y;
}
function volume()
{
return $this->x * $this->y* $this->y;
}
}
$r=new Rectangle(12, 4);
echo "<br> Area of Rectangle : ".$r->area();

$r=new Rectangle(12, 4,4);


echo "<br> volume of Rectangle : ".$r->volume(); */

/*class Cylinder extends Shape


{
function __construct($x, $y)
{
$this->x = $x;
$this->y = $y;
}
function area()
{
return $area=2*pi*$this->x*( $this->x+$this->y);
}
function volume()
{
return $vol=pi*$this->x*$this->x*$this->y;
}
}
$c=new Cylinder(8,9);
echo "<br> Area of Cylinder : ".$c->area();
echo "<br> volume of Cylinder : ".$c->volume();*/

class Circle extends Shape


{
function __construct($x)
{
$this->x = $x;
}
function area()
{
return pi*($this->x*$this->x);
}
function volume()
{
return four_thirds*pi*($this->x*$this->x);
}
}
$cr=new Circle(6,3);
echo "<br> Area of Circle : ".$cr->area();
echo "<br> volume of Circle : ".$cr->volume();
?>

Slip6: Write a PHP script, which will return the following component of the
URL (Using response header)
http://www.college.com/Science/CS.php
List of Components: scheme,
host,
path
Expected output:
Scheme: http
Host: www.college.com
Path: /Science/CS.php

<?php
header("Content-type:text/plain");
$url = 'http://www.college.com/Science/CS.php';

$url=parse_url($url);
echo 'Scheme : '.$url['scheme']."\n";
echo 'Host : '.$url['host']."\n";
echo 'Path : '.$url['path']."\n";
//var_dump($http_response_header);
?>

Slip7 : Define an Interface which has method gmtokg() &kgtogm(). Create


Class Convert which implements this interface & convert the value kg to gm
and gm to kg.
<?php
interface Example
{
function gmtokg();
function kgtogm();
}
class Convert implements Example
{
public $w;
function __construct($w)
{
$this->w=$w;
}
function gmtokg()
{
echo "Value in gram : " .$this->w . "<br>";
$this->w=$this->w/1000;
echo "Value in kg :" . $this->w . "Kilogram<br>";
}
function kgtogm()
{
echo "Value in Kilogram : " .$this->w . "<br>";
$this->w=$this->w*1000;
echo "Value in gms = " . $this->w . "gram<br>";
}
}
$c=new Convert(1000);
$c->gmtokg();

$c=new Convert(4);
$c->kgtogm();
?>

Slip 8: Write a PHP Script to create class Shape and its sub-class Triangle,
Square, Circle and display area of selected shape (use concept of inheritance).

<?php
define('pi',3.14);
define('four_thirds',4/3);
define('one_twos',1/2);

class Shape
{
public $x = 0;
public $y = 0;
}

/*class Circle extends Shape


{
function __construct($x)
{
$this->x = $x;
}
function area()
{
return pi*($this->x*$this->x);
}
}
$cr=new Circle(6,3);
echo "<br> Area of Circle : ".$cr->area();*/

/* class Triangle extends Shape


{
function __construct($x,$y)
{
$this->x = $x;
$this->y = $y;
}
function area()
{
return 1/2*($this->x*$this->y);
}
}
$tr=new Triangle(6,3);
echo "<br> Area of Triangle : ".$tr->area();*/

class Square extends Shape


{
function __construct($x)
{
$this->x = $x;
}
function area()
{
return $this->x*$this->x;
}
}
$sr=new Square(6);
echo "<br> Area of Square : ".$sr->area();
?>

Slip9: Write a PHP program to create a Class Calculator which will accept
two values from user and pass as an argument through parameterized
constructor and do the following task:
a)Add
b)Subtract
c) Multiply
them together or divide them on request.

Calform.html
<!doctype html>
<html>
<head>
</head>
<body>
<form action="calculator.php" method=get>
Enter 1st value :
<input type="text" name=a> <br><br><br>
Enter 2nd value :
<input type="text" name=b> <br><br><br>
<input type = "submit">
</form>
</body>
</html>

Calculator.php
<?php
class Calculate
{
public $a;
public $b;
function __construct($a,$b)
{
$this->a=$a;
$this->b=$b;
}
public function add()
{
$c=$this->a+$this->b;
echo"Addition = $c<br>";
}
public function subtract()
{
$c=$this->a-$this->b;
echo"Subtract = $c<br>";
}
public function multiply()
{
$c=$this->a*$this->b;
echo"Multiplication = $c<br>";
}
public function div()

{
$c=$this->a/$this->b;
echo"Division = $c";
}
}
$x=$_GET['a'];
$y=$_GET['b'];
$calc=new Calculate($x,$y);
$calc->add();
$calc->subtract();
$calc->multiply();
$calc->div();
?>

Slip10 : Write a PHP Script to demonstrate the concept of Introspection for


examining object. (Using any 3 predefined functions).
<?php
class demo
{
public $a,$b;
function show()
{
//echo "hi";
}

function sample()
{
}
}
$obj= new demo();
$obj->show();
print_r(get_class_methods("demo"));
print_r(get_declared_classes());
print_r(get_class_vars("demo"));
?>
Slip 11 : Write a PHP program to create class circle having radius data
member and two member functions find_circumference() and find_area().
Display area and circumference depending on user's preference.
<html>
<head>
<title>PHP Program To find Area and Circumference of a Circle</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td><input type="text" name="num1" value="" placeholder="Enter the radius of a
circle"/></td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$r = $_POST['num1'];
$pi = 3.14;
$area = $pi * $r * $r;
echo "Area of a Circle is: ".$area;
echo "<br>";
$cir = 2*$pi*$r;
echo "Circumference of a circle is: " .$cir;
return 0;
}
?>
</body>
</html>

Slip12 : Write a PHP program to convert temperature Fahrenheit to Celsius


using sticky form.
<html>
<head>
<title>
Temperature Conversion
</title>
</head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">


fahrenheit Temperature :
<input type="text" name="fahrenheit" value="<?php echo $fahr ?>">
<br>
<input type="submit" name="convert to celsius">
</form>

<?php
$fahr=$_GET[ 'fahrenheit' ];
if(! is_null($fahr))
{
$celsicus=($fahr-32)*5/9;
printf("%.2ff is %.2fc", $fahr, $celsicus);
}
?>
</body>
</html>

Slip13 : Create a form to accept Employee detail and display it in next


page. (Use sticky form concept).

<html>
<body>
<form method="POST" action="nextslip13.php">
Employee ID. :<input type="text" name="sno" value="<?php
if(isset($_POST['sno']))echo $_POST['sno'];?>"><br><br>

Employee Name :<input type="text" name="sname" value="<?php


if(isset($_POST['sname']))echo $_POST['sname'];?>"><br><br>

Employee Position :<input type="text" name="spos" value="<?php


if(isset($_POST['spos']))echo $_POST['spos'];?>"><br><br>

<input type="submit" name="submit" value="save"><br>


</form>
</body>
</html>

Nextslip13.php
<?php
if(isset($_POST['submit']))
{
$sno=$_POST['sno'];
$sname=$_POST['sname'];
$spos=$_POST['spos'];
echo "Employee id is:$sno<br>";
echo "Employee name is:$sname<br>";
echo "Employee position is:$spos<br>";
}
?>
Slip14 : Write a PHP script to accept a string from user and then display the
accepted string in reverse order. (use concept of self processing form)

<html>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Enter String : <input type="text" name="str1"><br><br>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$str=$_POST['str1'];
$nstr=strrev($str);
echo"The reverse string is : ".$nstr;
}
?>
</body>
</html>

Slip15 : Write PHP program to select list of subjects from list box and displays
the selected subject information on next page. (Use sticky Multivalued
parameter).

<html>
<body>
<form action="multicheckslip15.php" method="post">
Subjects:<select name="tech[]" size=6 multiple>
<option value="php">PHP</option>
<option value="asp">ASP</option>
<option value="jsp">JSP</option>
<option value="c++"> C++</option>
<option value="angularjs">ANGULAR JS</option>
<option value="dotnet"> DOT NET</option>
</select>
<br>
<input type="submit" value="Display">
</form>
</body>
</html>
Multicheckslip15.php
<?php
$tech = $_POST['tech'];
if(is_array($tech)) {
foreach($tech as $v) echo $v."<br>";
}
else echo $tech."<br>";
?>

Slip16 : Write a PHP program to accept two string from user and check
whether entered strings are matching or not.(use sticky form concept)
<html>
<head>
</head>
<body>
<form method="POST" action="slip16.php">
<b>Enter String 1:
</b><input type="text" name="str1" value="<?php if(isset($_POST['str1'])) echo
$_POST['str1'];?>" /><br><br>
<b>Enter String 2 :
</b><input type="text" name="str2" value="<?php if(isset($_POST['str2'])) echo
$_POST['str2'];?>" /><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
$s1 = $_POST['str1'];
$s2 = $_POST['str2'];
if(strcmp($s1,$s2) == 0)
{
echo "Matching...";
}
else {
echo "String not matching...";
}
}
?>
</body>
</html>

Slip17 : Write a PHP Script to display Server information in table format (Use
$_SERVER).
<?php
echo"<table border=1>";
foreach($_SERVER as $k=>$v)
{
echo"<tr><td>".$k."</td><td>".$v."</td></tr>";
}
echo"</table>";
?>
Slip18 : Write a PHP program to create a simple distance calculator that
can accept distance in meters from user. Convert it into centimeter or
kilometer according to user preference.
(use radio buttons and Self Processing form)
<html>
<head>
<title>Distance Conversion</title>
<body>
<?php
if($_SERVER['REQUEST_METHOD']=='GET')
{
?>
<form action="<?php echo$_SERVER['PHP_SELF']?>" method="POST">
Enter Distance in meter :<input type=text name=t1><br><br>
<input type="radio" name="r1" value="1">Convert into Centimeter<br><br>
<input type="radio" name="r1" value="2">Convert into Kilometer<br><br>
<input type="submit" value="Convert"><br>
</form>
<?php
}
else if($_SERVER['REQUEST_METHOD']=='POST')
{
$dist=$_POST['t1'];
$op=$_POST['r1'];
switch($op)
{
case 1:
echo "Conversion from meter to centimeter is :";
$cm=$dist*100;
echo "$cm cm";
break;
case 2:
echo "Conversion from meter to Kilometer is :";
$km=$dist/1000;
echo "$km km";
break;
}
}
?>
</body>
</html>

Slip20 : Write a PHP script for the following: Design a form to accept a
number from the user. Perform the operations and show the results.
l) Fibonacci Series.
2) To find sum of the digits of that number.
(Use the concept of self processing page.)
<?php
extract($_REQUEST);
if(isset($check))
{
$num = 0;
$n1 = 0;
$n2 = 1;
echo "<h3>Fibonacci series for first $n numbers: </h3>";
echo "\n";
echo $n1.' '.$n2.' ';
while ($num < $n-2 )
{
$n3 = $n2 + $n1;
echo $n3.' ';
$n1 = $n2;
$n2 = $n3;
$num = $num + 1;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter Your Number :
<input type="text" name="n" required>

<input type="submit" value="Print Fibonacci Series" name="check">

</form>
</body>
</html>

Slip30 : Derive a class Rectangle from class Square. Create one more class
Triangle. Create an interface with only one method called cal_area ().
Implement this interface in all the classes. Include appropriate data members
and constructors in all classes. Write a program to accept details of a Rectangle,
Square and Triangle and display the area.

<?php
$a ;
$z;
$r;
$s;
$w;
$l;
$res;
interface i1
{
public function cal_area();
}

class ractangle implements i1


{
public function __construct($a,$b)
{
$GLOBALS['w']=$a;
$GLOBALS['l']=$b;
}
public function cal_area ()
{
$GLOBALS['res']=$GLOBALS['w']* $GLOBALS['l'];
echo " <br> area of rectangle is: " .$GLOBALS['res'];
}
}

class square implements i1


{
public function __construct($n)
{
$GLOBALS['a']=$n;
}
public function cal_area()
{
$GLOBALS['z']=$GLOBALS['a'] * $GLOBALS['a'];
echo " <br> area of square is:" .$GLOBALS['z'];
}
}
class triangle implements i1
{
public function __construct($x1,$y1)
{
$GLOBALS['x']=$x1;
$GLOBALS['y']=$y1;
}
public function cal_area ()
{
$GLOBALS['s']=1/2 *$GLOBALS['x']* $GLOBALS['y'];
echo "<br>area of triangle is: " .$GLOBALS['s'];
}
}
$obj=new square(10);
$obj->cal_area();
$obj=new triangle(2,4);
$obj->cal_area();
$obj=new ractangle(2,3);
$obj->cal_area();
?>

You might also like