Simple Calculator using PHP

PHP / Class, Object and Methods

176

Program:

<?PHP
class Calculator {
   
    public function add($number1, $number2){
       if(is_numeric($number1) && is_numeric($number2)){
           return $number1+$number2;
       }else{
           echo "values should be numeric.";
       }
    }
 
    public function subtract($number1, $number2){
        if(is_numeric($number1) && is_numeric($number2)){
             return $number1-$number2;
        }else{
              echo "values should be numeric.";
        }
    }
 
    public function multiply($number1, $number2){
        if(is_numeric($number1) && is_numeric($number2)){
              return $number1*$number2;
        }else{
             echo "values should be numeric.";
        }
    }
 
    public function divide($number1, $number2) {
          if(is_numeric($number1) && is_numeric($number2) && $number2 != 0){
            return $number1/$number2;
          }else{
             echo "Values should be numeric and divisor shouldn't be zero.";
          }
    }
}
 
$calc = new Calculator;
 
echo $calc->add(1,3);
echo "<br>";
echo $calc->subtract(4,3);
echo "<br>";
echo $calc->multiply(2,3);
echo "<br>";
echo $calc->divide(10,2);
 
?>

Output:

4
1
6
5

Explanation:


This Particular section is dedicated to Programs only. If you want learn more about PHP. Then you can visit below links to get more depth on this subject.