Object Oriented PHP(php oop) Part-02 (php calculator in oop) - Web Development and Web Design Codes

Latest

Sunday, February 4, 2018

Object Oriented PHP(php oop) Part-02 (php calculator in oop)

PHP Calculator in OOP

Object Oriented PHP(php oop) Part-02 (php calculator in oop)
Object Oriented PHP(php oop) Part-02 (php calculator in oop)

In this lesson we are going to learn about how to create calculator in php oop.
it's will help you to understand php class,property and object.
create 2 files named:
1.index.php
2.function.php

Step 1: Codes for Index.php
HTML codes top of the page
<html>
<head>
<title>Working with Method and Object in PHP OOP</title>
</head>
<form method="POST" action="">
<span>Enter First Number:</span><br/>
  <input type="number" name="num1" required/><br >
  <span>Enter Seconds Number</span><br >
  <input type="number" name="num2" required/><br >
  <input type="submit" name="submit" value="Calculation"/>
</form>
</html>


Add the below php codes end of the html
<?php
include("function.php");
if(isset($_POST["submit"])){
 $numOne=$_POST['num1'];
 $numTwo=$_POST['num2'];
if(empty($numOne) or empty($numTwo)){
 echo "Fields are Mandatory";
}else{
 echo "Your First Number is: $numOne <br/> Seconds Number is: $numTwo <br/>";
 //Calling Class Calculator
 $obj=new Calculator;
 //
 $obj->add($numOne,$numTwo);
 $obj->sub($numOne,$numTwo);
 $obj->mul($numOne,$numTwo);
 $obj->div($numOne,$numTwo);
}
}
 
?>
Step 2: Codes for function.php :
<?php
class Calculator{
  
  public function add($a,$b){
   echo 'Summation: '.($a+$b).'<br/ > ';
  }
  public function sub($a,$b){
   echo 'Subtraction: '.($a-$b).'<br/ > ';
  }
  public function mul($a,$b){
   echo 'Multiplication: '.($a*$b).'<br/ > ';
  }
  public function div($a,$b){
   echo 'Division: '.($a/$b);
  }
 
}

?>
Result:
Object Oriented PHP(php oop) Part-02 (php calculator in oop)
Object Oriented PHP(php oop) Part-02 (php calculator in oop)

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.