This gives the same result as the previous post, however this introduces the use of an object to learn the basics of OOP in PHP .
<?php
class Calculator
{
public $num1;
public $num2;
public function doAddition()
{
return ($this->num1+$this->num2);
}
public function doSubtraction()
{
return ($this->num1-$this->num2);
}
public function doMultiplication()
{
return ($this->num1*$this->num2);
}
public function doDivision()
{
return ($this->num1/$this->num2);
}
}
?>
<html>
<head><title>PHP calculator</title></head>
<body>
<?php
$calculator = new Calculator();
$calculator->num1 =$_POST['num1'];
$calculator->num2 =$_POST['num2'];
$equal = "";
$submitAdd =$_POST['submitAdd'];
$submitSub =$_POST['submitSub'];
$submitMul =$_POST['submitMul'];
$submitDiv =$_POST['submitDiv'];
if($submitAdd!=""){
$equal = $calculator->doAddition();
$operation = "+";
}
else if($submitSub!=""){
$equal =$calculator->doSubtraction();
$operation = "-";
}
else if($submitMul!=""){
$equal =$calculator->doMultiplication();
$operation = "*";
}
else if($submitDiv!=""){
$equal =$calculator->doDivision();
$operation = "/";
}
?>
<form name="form1" action="" method="post">
<p>CALCULATOR</p>
<p>
<input type="text" name="num1" size="1" value="<?php if($calculator->num1){ echo $calculator->num1;}?>"/>
<input type="text" name="num2" size="1" value="<?php if($calculator->num2){ echo $calculator->num2;}?>"/>
<input type="submit" name="submitAdd" id="button" value="add"/>
<input type="submit" name="submitSub" id="button" value="sub"/>
<input type="submit" name="submitMul" id="button" value="mul"/>
<input type="submit" name="submitDiv" id="button" value="div"/>
</p>
</form>
<?php
if($equal != "") echo "$calculator->num1 $operation $calculator->num2 = $equal";
?>
</body>
</html>
<?php
class Calculator
{
public $num1;
public $num2;
public function doAddition()
{
return ($this->num1+$this->num2);
}
public function doSubtraction()
{
return ($this->num1-$this->num2);
}
public function doMultiplication()
{
return ($this->num1*$this->num2);
}
public function doDivision()
{
return ($this->num1/$this->num2);
}
}
?>
<html>
<head><title>PHP calculator</title></head>
<body>
<?php
$calculator = new Calculator();
$calculator->num1 =$_POST['num1'];
$calculator->num2 =$_POST['num2'];
$equal = "";
$submitAdd =$_POST['submitAdd'];
$submitSub =$_POST['submitSub'];
$submitMul =$_POST['submitMul'];
$submitDiv =$_POST['submitDiv'];
if($submitAdd!=""){
$equal = $calculator->doAddition();
$operation = "+";
}
else if($submitSub!=""){
$equal =$calculator->doSubtraction();
$operation = "-";
}
else if($submitMul!=""){
$equal =$calculator->doMultiplication();
$operation = "*";
}
else if($submitDiv!=""){
$equal =$calculator->doDivision();
$operation = "/";
}
?>
<form name="form1" action="" method="post">
<p>CALCULATOR</p>
<p>
<input type="text" name="num1" size="1" value="<?php if($calculator->num1){ echo $calculator->num1;}?>"/>
<input type="text" name="num2" size="1" value="<?php if($calculator->num2){ echo $calculator->num2;}?>"/>
<input type="submit" name="submitAdd" id="button" value="add"/>
<input type="submit" name="submitSub" id="button" value="sub"/>
<input type="submit" name="submitMul" id="button" value="mul"/>
<input type="submit" name="submitDiv" id="button" value="div"/>
</p>
</form>
<?php
if($equal != "") echo "$calculator->num1 $operation $calculator->num2 = $equal";
?>
</body>
</html>
No comments:
Post a Comment