calculator - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of calculator.php

  1. $operation = readline(“Welke operatie wil je uitvoeren? (+, -, %));
  2. $op1 = "+";
  3. $op2 = "-";
  4. $op3 = "%";
  5.  
  6. if ($operation == $op1) {
  7.     echo (“Optellen”) . PHP_EOL;
  8. } elseif ($operation == $op2) {
  9.     echo (“Aftrekken”) . PHP_EOL;
  10. } elseif ($operation == $op3) {
  11.     echo (%) . PHP_EOL;
  12. } else {
  13.     exit(“geen geldige operatie” . PHP_EOL);
  14. }
  15.  
  16. $a = readline(“Je eerste getal?” . PHP_EOL);
  17. if (!is_numeric($a)){
  18.     exit(“‘” . $a . “’ is geen getal” . PHP_EOL);
  19. }
  20. $b = readline(“Je tweede getal?” . PHP_EOL);
  21. if (!is_numeric($b)){
  22.     exit(“‘” . $b . “’ is geen getal” . PHP_EOL);
  23. }
  24. if ($operation == (+)) {
  25.     $sum1 = $a + $b;
  26.     echo (“Het resultaat is: ” . $sum1) . PHP_EOL;
  27. } elseif ($operation == (-)) {
  28.     $sum2 = $a - $b;
  29.     echo (“Het resultaat is: ” . $sum2) . PHP_EOL;
  30. } elseif ($operation == (%)) {
  31.     $sum3 = $a % $b;
  32.     echo (“Het resultaat is: ” . $sum3) . PHP_EOL;
  33. }
  34. ?>
File Description
  • calculator
  • PHP Code
  • 12 Sep-2021
  • 973 Bytes
You can Share it: