Advance PHP
Slip-3 a) Write a Calculator class that can accept two values, then add them, subtract them, multiply them together, or divide them on request.
For example:
$calc = new Calculator(3, 4);
echo $calc->add(); // Displays "7"
echo $calc->multiply(); // Displays “12"
<HTML>
<BODY>
<FORM method="get" action="#">
Enter first value: <input type="text" name="a">
<br><br>
Enter second value:<input type="text" name="b"><br><br>
<input type="submit" name="submit">
</FORM>
<hr>
</BODY>
</HTML>
<!-- calculate.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();
?>
Advance PHP
Slip No-4 a) Define a class Employee having private members id, name, department, salary. Define parameterized constructors. Create a subclass called "Manager" with private member bonus. Create 6 objects of the Manager class and display the details of the manager having the maximum total salary (salary + bonus).
<?php
class Employee
{
private $empid,$empname, $empdept,$salary;
function __construct($a,$b,$c,$d)
{
$this->empid=$a;
$this->empname=$b;
$this->empdept=$c;
$this->salary=$d;
}
public function getdata()
{
return $this->salary;
}
public function display()
{
echo $this->empid." ";
echo $this->empname." ";
echo $this->empdept." ";
}
}
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)
{
$salary=$this->getdata();
$total=$salary+$this->bonus;
if($total>self::$total1)
{
self::$total1=$total;
return $this;
}
else
{
return $ob;
}
}
public function display()
{
parent::display();
echo self::$total1;
}
}
$ob=new Manager(0,"ABC","",0,0);
$ob1=new Manager(1,"Mahesh","Developer",280000,7000);
$ob=$ob1->max($ob);
$ob2=new Manager(2,"Aakash","Tester",30000,2500);
$ob=$ob2->max($ob);
$ob3=new Manager(3,"Amol","PM",32000,3000);
$ob=$ob3->max($ob);
$ob->display();
?>
Advance PHP Slip No-5
Slip No-5 a) 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
{
public $x = 0;
public $y = 0;
public $l = 0;
public $b= 0;
public $h = 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 setlbh(){
$this->l=5;$this->b=4;$this->h=6;
}
function volume()
{
return $this->l*$this->b*$this->h;
}
}
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;
}
}
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);
}
}
$c = new cylinder(12,4);
echo "<br>Cylinder Area: ".$c->area();
echo "<br>Cylinder Volume: ".$c->volume();
echo "<br>--------------------------------------------------<br>";
$r=new Rectangle(12,4);
echo "<br>Rectangle Area : ".$r->area();
$r->setlbh();
echo "<br>Rectangle Volume : ".$r->volume();
echo "<br>--------------------------------------------------<br>";
$circle= new Circle(12);
echo "<br>Circle Area : ".$circle->area();
echo "<br>Circle Volume: ".$circle->volume();
?>
Advance PHP Slip No-8
Slip No-8 a) Write a PHP script to create class shape and its sub-class Triangle, Square, Circle and display area of selected shape(use concept of inheritence).
<?php
class Shape {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function area() {
echo "Area of " . $this->name . " is not defined.<br>";
}
}
class Triangle extends Shape {
public $base;
public $height;
public function __construct($base, $height) {
parent::__construct("Triangle");
$this->base = $base;
$this->height = $height;
}
public function area() {
$area = 0.5 * $this->base * $this->height;
echo "Area of " . $this->name . " is " . $area . "<br>";
}
}
class Square extends Shape {
public $side;
public function __construct($side) {
parent::__construct("Square");
$this->side = $side;
}
public function area() {
$area = $this->side * $this->side;
echo "Area of " . $this->name . " is " . $area . "<br>";
}
}
class Circle extends Shape {
public $radius;
public function __construct($radius) {
parent::__construct("Circle");
$this->radius = $radius;
}
public function area() {
$area = 3.14 * $this->radius * $this->radius;
echo "Area of " . $this->name . " is " . $area . "<br>";
}
}
$triangle = new Triangle(10, 5);
$triangle->area();
$square = new Square(7);
$square->area();
$circle = new Circle(3);
$circle->area();
?>
Advance PHP
Slip No:15 a) Write PHP to select list of subjects from list box and displays the selected subject information on next page.(Use Sticky Multivalued Parameter).
selectsubject.php
<html>
<body>
<form action="subject.php" method="post">
<h1>Select Subjects</h1><br><br>
<select name="subjects[]" multiple>
<option value="Marathi">Marathi</option>
<option value="Data Structure">Data Structure</option>
<option value="Sanskrut">Sanskrut</option>
<option value="C++">C++</option>
<option value="Advance PHP">Advance PHP</option>
<option value="DBMS">DBMS</option>
<option value="RDBMS">RDBMS</option>
<option value="C Language">C Language</option>
</select><br>
<input type="submit" name="submit">
</form>
</body>
</html>
subject.php
<?php
if(isset($_POST['submit'])) {
$selectedSubjects=$_POST['subjects'];
if(empty($selectedSubjects)) {
echo "Please Select Subject First.";
} else {
echo "Selected subjects Are:<br>";
foreach($selectedSubjects as $subject) {
echo $subject."<br>";
}}}
?>
Advance PHP
Slip No:16 a) Write a PHP program to accept two string from user and check whether entered strings are matching or not.(use sticky form concept).
<html>
<body>
<form action="#" method="post">
Enter First String:<input type="text" name="first" value="<?php echo ($_POST['first']); ?>"><br>
Enter Second String:<input type="text" name="second" value="<?php echo ($_POST['second']); ?>"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
$first=$_POST['first'];
$second=$_POST['second'];
if($first==$second){
echo"Entered Strings are Matched";
}else{
echo"Entered Strings are Not Matched";
}
?>
Advance PHP
Slip No: 17 a) Write a PHP script to display Server Information in table format. (Use $_SERVER)
<?php
$server=$_SERVER;
echo"<table border='1'>";
foreach($server as $menu => $description){
echo "<tr>";
echo "<td>" . $menu . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
}
echo"</table>";
?>
. Advance PHP
Slip No: 18 a) 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's preference.
<html>
<body>
<h1>Distance Calculator</h1>
<form action="#" method="post">
Enter Distance:<input type="number" name="meter"> Meter<br>
Centimeter: <input type="radio" name="cm"><br>
Kilometer: <input type="radio" name="km"><br><br>
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
$meter=$_POST['meter'];
if(isset($_POST['cm'])){
$result=$meter*1000;
echo"Converted Value= ".$result."cm";
exit;
}
if(isset($_POST['km'])){
$result=$meter/1000;
echo"Converted Value= ".$result."km";
}
?>
. Advance PHP
Slip No:19 a)Write a PHP Program to read the XML document "stock_list.xml"(fruits and vegetables) which creates XML document and parse the XML data into an array.
stock_list.xml
<?xml version="1.0" encoding="utf-8"?>
<stock_list>
<fruits>
<fruit>Apple</fruit>
<fruit>Grapes</fruit>
<fruit>Orange</fruit>
</fruits>
<vegetables>
<vegetable>Potato</vegetable>
<vegetable>Broccoli</vegetable>
<vegetable>Spinach</vegetable>
</vegetables>
</stock_list>
slip19.php
<?php
$xml = simplexml_load_file('stock_list.xml');
$parsed_data = [$xml];
print_r($parsed_data);
?>
Advance PHP
Slip No:20) a) Create a XML file which gives details of movies available in "Venus CD Store" from following
categories a)Classical b)action c)Horror
Elements in each category are in the following format.
<Category>
<Movie_Name></Movie_Name>
<Release_Year></Release_Year>
<Actor_Name></Actor_Name>
</Category>
save the file with name "movies.xml".
<?xml version="1.0" encoding="utf-8"?> //write on first line is mandatory
<Category>
<Movie_Name></Movie_Name>
<Release_Year></Release_Year>
<Actor_Name></Actor_Name>
</Category>
//Save the file with name "movies.xml"
<details>
<Category name="Classical">
<Movie_Name>Sholey</Movie_Name>
<Release_Year>1975</Release_Year>
<Actor_Name>Amitabh Bachchan</Actor_Name>
</Category>
<Category name="action">
<Movie_Name>RRR</Movie_Name>
<Release_Year>2022</Release_Year>
<Actor_Name>S.S.Rajamouli</Actor_Name>
</Category>
<Category name="Horror">
<Movie_Name>Tumbbad</Movie_Name>
<Release_Year>2018</Release_Year>
<Actor_Name>Piyush Kaushik</Actor_Name>
</Category>
</details>
Advance PHP
Slip No:21 a)Write a script to create XML file named "Course.xml".
<course>
<computer_science>
<student_name></student_name>
<class_name></class_name>
<percentage></percentage>
</computer_science>
</course>
store the details of 5 students who are in SYBBA(CA).
<?php
$xmlString = '<course>
<computer_science>
<student_name>Mahesh</student_name>
<class_name>SYBCA</class_name>
<percentage>90%</percentage>
</computer_science>
<computer_science>
<student_name>Amol</student_name>
<class_name>SYBCA</class_name>
<percentage>93%</percentage>
</computer_science>
<computer_science>
<student_name>88%</student_name>
<class_name>SYBCA</class_name>
<percentage>43</percentage>
</computer_science>
<computer_science>
<student_name>Rohan</student_name>
<class_name>SYBCA</class_name>
<percentage>95%</percentage>
</computer_science>
<computer_science>
<student_name>Lakshman</student_name>
<class_name>SYBCA</class_name>
<percentage>99%</percentage>
</computer_science>
</course>';
file_put_contents('Course.xml', $xmlString);
echo"<br>Course.xml File Created Successfully!";
//Check File in Your Folder where you save all php programs
?>
Advance PHP
Slip No:22) a) Create a XML file which gives details of books available in "Pragati Bookstore" from following categories.
1)Yoga
2)Story
3)Technical
and Elements in each category are in the following format.
<Book>
<Book_Title>...............</Book_Title>
<Book_Author>...............</Book_Author>
<Book_Price>...............</Book_Price>
</Book>
Save the file as "Bookcategory.xml".
<?xml version="1.0" encoding="utf-8"?>
<details>
<category name="Yoga">
<Book>
<Book_Title>The Language of Yoga</Book_Title>
<Book_Author>Shri. Swami Satchidananda</Book_Author>
<Book_Name>The Yoga Sutras of Patanjali</Book_Name>
</Book>
</category>
<category name="Story">
<Book>
<Book_Title>Harry Potter and the Sorcerer's Stone</Book_Title>
<Book_Author>J.K. Rowling</Book_Author>
<Book_Name>Harry Potter</Book_Name>
</Book>
</category>
<category name="Technical">
<Book>
<Book_Title>The Promise and the Peril of the Digital Age</Book_Title>
<Book_Author>Brad Smith</Book_Author>
<Book_Name>Tools and Weapons</Book_Name>
</Book>
</category>
</details>
Advance PHP
Slip No:23) a) Create an application that reads "Sports.xml" file into simple XML object. Display attributes and elements.(Hint: Use simplexml_load_file() function).
sports.xml
<?xml version="1.0" encoding="utf-8"?>
<sports>
<sport type="Ground Sport">
<name>Cricket</name>
<players>11</players>
</sport>
<sport type="In-Door Sport">
<name>Chess</name>
<players>2</players>
</sport>
</sports>
slip23.php
<?php
$xml=simplexml_load_file('sports.xml');
foreach($xml->sport as $sport){
echo"Sport type: ".$sport['type']."<br>";
echo"Name: ".$sport->name."<br>";
echo"Players: ".$sport->players."<br>";
}
?>
.
.
.
.
.
.
No comments:
Post a Comment