Tuesday, 3 May 2022

Advance PHP SLIP1Write 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)

calculator.html

 

<HTML>

 

<BODY>

 

<FORM method=get action="Calculate.php">

 

Enter first value: <INPUT type=text name="a"><br>

 

Enter second value:<INPUT type=text name="b"><br>

 

<INPUT type=submit>

 

</FORM>

 

</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();

 

?>

<?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();

 

?>