Advertisement
ThemePackNet

index.php - Usage of class, methods

Nov 2nd, 2021
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1.  
  2. <form action="" method="post">
  3.     <input type="number" name="num1" placeholder="Enter the first number"><br><br>
  4.     <input type="number" name="num2" placeholder="Enter the second number"><br><br>
  5.     <input name="calculation" type="submit" value="Calculate"><br><br>
  6. </form>
  7.  
  8. <?php
  9. class Calculator
  10. {
  11.     public function add($num1, $num2)
  12.     {
  13.         echo "Summation: " . ($num1 + $num2) . "<br>";
  14.     }
  15.     public function deduct($num1, $num2)
  16.     {
  17.         echo "Deduction: " . ($num1 - $num2) . "<br>";
  18.     }
  19. }
  20.  
  21. if(isset($_POST['calculation'])){
  22.     $num1 = $_POST['num1'];
  23.     $num2 = $_POST['num2'];
  24.  
  25.     if(empty($num1) || empty($num2)){
  26.         echo "<p style='color:#ee6554'>Fields can not be empty!</p>";
  27.     } else {
  28.         $calc = new Calculator();
  29.         $calc->add($num1, $num2);
  30.         $calc->deduct($num1, $num2);
  31.     }
  32. }
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement