Advertisement
HristoBaychev

Untitled

Mar 17th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. <?php
  2. declare (strict_types=1); // declare - strict = 1 use for variables
  3.  
  4.  
  5. function sum($a, $b) : float { // name of function (arg1 , arg2) ... : int - shut be int
  6. $sum = $a + $b; // function of function :)
  7. return $sum; // return the result
  8. } // dont forget {} after function
  9.  
  10.  
  11. function subtraction($a, $b) : float {
  12. $subtraction = $a - $b;
  13. return $subtraction;
  14. }
  15.  
  16. function multiplication($a, $b) : float {
  17. $multiplication = $a * $b;
  18. return $multiplication;
  19. }
  20.  
  21. function division($a, $b) : float {
  22. $division = $a / $b;
  23. return $division;
  24. }
  25.  
  26. function stepen($a, $b) : float {
  27. $pow = $a ** $b;
  28. return $pow;
  29. }
  30.  
  31. ?>
  32.  
  33.  
  34. <form method = "get">
  35. <b>Number one:</b> <input type = "number" name = "num1" value = "some number"/><br/>
  36. <b>Symbol</b><input type = "symbol" name = "delimiter" value = "delimiter"/><br/>
  37. <b>Number two:</b><input type = "number" name = "num2" value = "some number"/><br/>
  38. <input type = "submit" name = "sum">
  39. </form>
  40.  
  41.  
  42. <?php
  43.  
  44. if (isset($_GET['sum'])){ // check if have smth in 'sum' from user
  45. $a = filter_var($_GET['num1'], FILTER_VALIDATE_FLOAT); // every time we need to PARSE the type int, str, float for variable !!! IF IS NOT USED declare(strict_types=1) IS NOT NECESSARY TO PARSE
  46. $symbol = $_GET['delimiter'];
  47. $b = filter_var($_GET['num2'], FILTER_VALIDATE_FLOAT); // every time we need to PARSE the type int, str, float for variable !!! IF IS NOT USED declare(strict_types=1) IS NOT NECESSARY TO PARSE
  48. //echo sum($a, $b); // print result
  49. if ($a === false || $b === false){
  50. echo "Invalid input!";
  51. }
  52.  
  53. if ($symbol === "+"){
  54. echo sum($a, $b);
  55. }
  56. elseif ($symbol === "-") {
  57. echo subtraction($a, $b);
  58. }
  59. elseif ($symbol === "*") {
  60. echo multiplication($a , $b);
  61. }
  62. elseif ($symbol === "/") {
  63. echo division($a, $b);
  64. }
  65. elseif ($symbol === "**") {
  66. echo stepen($a, $b);
  67. }
  68. }
  69.  
  70. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement