Thursday, April 13, 2023

Advance PHP Slips Semester 4

 Advance PHP


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).
<?php
class MyCalculator
{
private $fval, $sval;
public function __construct( $fval, $sval)
{
$this->fval = $fval;
$this->sval= $sval;
}
public function add()
{
return $this->fval + $this->sval;
}
public function subtract()
{
return $this->fval - $this->sval;
}
public function multiply()
{
return $this->fval * $this->sval;
}
public function divide()
{
return $this->fval / $this->sval;
}
}
$mycalc = new MyCalculator(18, 3);
echo "Addition: ",$mycalc-> add()."\n <br>";
echo"Multiplication: ",$mycalc->multiply()."\n <br>";
echo "Subtraction: ",$mycalc-> subtract()."\n<br>";
echo "Divide: ",$mycalc-> divide()."\n <br>";
?>

 Advance PHP

Slip 2. a) 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 Myclass
{
public $a;
public $b=1;
public $c='Mahesh';

function Myclass()
{
    //myclass function
}
function myfun1()
{
    //function
}
function myfun2(){
    //function
}
}
$class=get_declared_classes();
foreach($class as $cname)
{
    echo"$cname<br>";
}
echo"<br>Class Methods are : <br>";
$m=get_class_methods('Myclass');
foreach($m as $mname)
{
    echo"$mname<br>";
}
$cp=get_class_vars('Myclass');
echo"class variables are :<br>";
get_declared_classes();
foreach($cp as $cpname => $v)
{
   
    echo"$cpname: $v<br>";
}
?>

 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-6 a) 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";
//OR
//var_dump($http_response_header);
?>

 Advance PHP Slip No-7

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 gm= ".$this->w." Gram<br>";
    $this->w=$this->w/1000;
    echo"Value in kg= ".$this->w."Kilogram<br>";
}

function kgtogm(){
echo"Value in kg= ".$this->w." Kilogram<br>";
$this->w=$this->w*1000;
echo"Value in gm= ".$this->w."Gram<br>";
}
};

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


 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-9

a)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) Substract 

c) Multiply them together or divide them on request.


<html>
    <body>
        <form action="#" method="post">
            Enter First Number:<input type="number" name="first"><br>
            Enter Second Number:<input type="number" name="second"><br>
            <input type="submit" name="submit">
        </form>
    </body>
</html>
<?php

class Calculator {
    private $num1;
    private $num2;

    public function __construct($num1, $num2) {
        $this->num1 = $num1;
        $this->num2 = $num2;
    }

    public function add() {
        $result = $this->num1 + $this->num2;
        echo "Addition of " . $this->num1 . " and " . $this->num2 . " is " . $result . "<br>";
    }

       public function sub() {
        $result = $this->num1 - $this->num2;
        echo "Subtraction of " . $this->num1 . " and " . $this->num2 . " is " . $result . "<br>";
    }

   
    public function mult() {
        $result = $this->num1 * $this->num2;
        echo "Multiplication of " . $this->num1 . " and " . $this->num2 . " is " . $result . "<br>";
    }

   
    public function div() {
        if ($this->num2 == 0) {
            echo "Cannot divide by zero.<br>";
        } else {
            $result = $this->num1 / $this->num2;
            echo "Division of " . $this->num1 . " by " . $this->num2 . " is " . $result . "<br>";
        }
    }
}


$num1 = $_POST['first'];
$num2 = $_POST['second'];
$calc = new Calculator($num1, $num2);

$calc->add();
$calc->sub();
$calc->mult();
$calc->div();

?>


 Advance PHP

Slip No-10 a) Write PHP script to demonstrate the concept of introspection for examining object.


<?php
class Myclass
{
    public $a;
    public $b=1;
    public $c='Mahesh';
    function Myclass(){

    }
    function myfun1(){ }
    function myfun2(){ }
}

$class=get_declared_classes();
foreach($class as $cname)
{
    echo"$cname<br>";
}
echo"<br>Class Methods are : <br>";
$m=get_class_methods('Myclass');
foreach($m as $mname)
{
    echo "$mname<br>";
}
$cp=get_class_vars('Myclass');
echo"class variables are :<br>";
foreach($cp as $cpname => $v){
echo"$cpname : $v <br>";
}
?>


 Advance PHP 

Slip No: 11 a)

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>
    <body>
        <form action="#" method="post">
            Enter Radius:<input type="text" name="text">
            <input type="submit" name="submit">
        </form>
    </body>
</html>
<?php
class circle{
    public $radius;
    public function area_of_circle($radius){
    $a=3.14159*$radius*$radius;
    echo"<br>Area of Circle is:".$a;
}
public function circumference_of_circle($radius){
    $a=2*3.14159*$radius;
    echo"<br>Circumference of Circle is:".$a;
    }
}
$m=$_POST['text'];
$obj=new circle();
$obj->area_of_circle($m);
$obj->circumference_of_circle($m);
?>


 Advance PHP

Slip No:12 a) Write a PHP program to convert temperature Fahrenheit to Celsius using Sicky Form.


<html>
    <body>
        <form action="#" method="post">
            Enter Temperature in Fahrenheit:
            <input type="number" name="fah" value="<?php echo($_POST['fah']); ?>">
            <input type="submit" name="submit">
        </form>      
    </body>
</html>
   
<?php
    $f = $_POST['fah'];
    $c = ($f-32)*5/9;
    echo "<br>Converted Value is:".$c." C";
?>

 Advance PHP

Slip No:13 a) Create a form to accept Employee detail and display it in next page. (Use Sticky Form Concept).

employee.php


<html>
    <body>
        <form action="emp.php" method="get">
            Enter Employee Id:<input type="number" name="id" value="<?php echo ($_GET['id']); ?>"><br>
            Enter Name of Employee:<input type="text" name="emp_name" value="<?php echo ($_GET['emp_name']); ?>"><br>
            Enter Name of Department:<input type="text" name="emp_dept" value="<?php echo ($_GET['emp_dept']);; ?>"><br>
            Enter Age of Employee:<input type="number" name="emp_age" value="<?php echo ($_GET['emp_age']);; ?>"><br>
            <input type="submit" name="submit">
        </form>
    </body>
</html>

emp.php


<?php
$empid=$_GET['id'];
$empname=$_GET['emp_name'];
$empdept=$_GET['emp_dept'];
$empage=$_GET['emp_age'];
echo"<br>Employee Id=".$empid;
echo"<br>Employee Name:".$empname;
echo"<br>Employee Department:".$empdept;
echo"<br>Employee Age:".$empage;
?>

 Advance PHP

Slip No: 14 a) 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 action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Enter String:<input type="text" name="str">
    <input type="submit" name="submit">
    </form>
    <?php
$string=$_POST['str'];
echo strrev($string);
    ?>
    </body>
</html>


 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>";
}
?>





.




.

 






.


.





.









.