Advertisement
gurumutant

PHP - Demo Operator Aritmatika

Mar 25th, 2024
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1. <?php
  2.     // mengecek pengiriman form
  3.     if (isset($_POST['opr'])) {
  4.         // echo "<pre>"; print_r($_POST); echo "</pre>";  
  5.         extract($_POST);
  6.         if (is_numeric($bil1) && is_numeric($bil2)) {
  7.             $hasil =  $bil1." ".$opr." ".$bil2. " = ";
  8.             switch($opr) {
  9.                 case "+": $hasil .= $bil1+$bil2; break;
  10.                 case "-": $hasil .= $bil1-$bil2; break;
  11.                 case "*": $hasil .= $bil1*$bil2; break;
  12.                 case "/": $hasil .= $bil1/$bil2; break;
  13.                 case "%": $hasil .= $bil1%$bil2; break;
  14.                 case "**": $hasil .= $bil1**$bil2; break;
  15.                 default: echo 'Operator salah!';
  16.             }
  17.         } else {
  18.             $error = "Operand yang diinputkan bukan bilangan!";
  19.         }
  20.     }
  21. ?>
  22. <!DOCTYPE html>
  23. <html lang="en">
  24. <head>
  25.     <meta charset="UTF-8">
  26.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  27.     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
  28.     <title>Document</title>
  29. </head>
  30. <body>
  31.     <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
  32.         <!-- <label for="bil1">Bilangan 1</label> -->
  33.         <input placeholder="bilangan 1" type="text" size="8" name="bil1">
  34.         <select name="opr">
  35.             <option value="+">+</option>
  36.             <option value="-">-</option>
  37.             <option value="*">*</option>
  38.             <option value="/">/</option>
  39.             <option value="%">%</option>
  40.             <option value="**">**</option>
  41.         </select>
  42.         <!-- <label for="bil2">Bilangan 2</label> -->
  43.         <input placeholder="bilangan 2" type="text" size="8" name="bil2">
  44.         <input type="submit" value="Hitung">
  45.     </form>
  46.     <?php if (isset($hasil)) { ?>
  47.         <div class="alert alert-success" role="alert">
  48.             <?= $hasil; ?>
  49.         </div>
  50.     <?php } ?>
  51.     <?php if (isset($error)) { ?>
  52.         <div class="alert alert-danger" role="alert">
  53.             <?= $error; ?>
  54.         </div>
  55.     <?php } ?>
  56.     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
  57. </body>
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement