Advertisement
gurumutant

PHP - Demo Operator Logika

Mar 25th, 2024
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.89 KB | None | 0 0
  1. <?php
  2.     // mengecek pengiriman form
  3.     if (isset($_POST['submit'])) {
  4.         // echo "<pre>"; print_r($_POST); echo "</pre>";
  5.         // mengekstraksi array $_POST menjadi variabel individual
  6.         extract($_POST);
  7.         // mengeset a dan b dari aktif tidaknya switcher
  8.         $a = (isset($a)) ? true : false;
  9.         $b = (isset($b)) ? true : false;
  10.         // pengecekan pengguna memilih operator atau tidak
  11.         if (isset($opr)) {
  12.             // mendapatkan hasil berdasarkan operator yang dipilih
  13.             switch ($opr) {
  14.                 case 'and': $hasil = ($a && $b); break;
  15.                 case 'or': $hasil = ($a || $b); break;
  16.                 case 'xor': $hasil = ($a xor $b); break;
  17.                 default: unset($hasil);
  18.             }
  19.         } else {
  20.             $error = "Anda tidak memilih operator !";
  21.         }
  22.     }
  23. ?>
  24. <!DOCTYPE html>
  25. <html lang="en">
  26. <head>
  27.     <meta charset="UTF-8">
  28.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  29.     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
  30.     <title>Document</title>
  31. </head>
  32. <body>
  33.     <form method="post">
  34.         <div class="form-check form-switch">
  35.             <input <?= isset($a) ? ($a?'checked':''):''; ?> name="a" value="1" class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault">
  36.             <label class="form-check-label" for="flexSwitchCheckDefault">A</label>
  37.         </div>
  38.         <div class="form-check form-switch">
  39.             <!-- <input <?= isset($b) ? ($b?'checked':''):''; ?> name="b" value="1" class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault"> -->
  40.             <input <?php
  41.                 if (isset($b)) {
  42.                     if ($b == true) {
  43.                         echo 'checked';
  44.                     }
  45.                 }
  46.             ?> name="b" value="1" class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault">
  47.             <label class="form-check-label" for="flexSwitchCheckDefault">B</label>
  48.         </div>
  49.         <input <?= (isset($opr) && ($opr == 'and')) ? 'checked':''; ?> type="radio" class="btn-check" name="opr" id="option5" value="and" autocomplete="off">
  50.         <label class="btn btn-outline-success" for="option5">and</label>
  51.         <input <?= (isset($opr) && ($opr == 'or')) ? 'checked':''; ?> type="radio" class="btn-check" name="opr" id="option6" value="or" autocomplete="off">
  52.         <label class="btn btn-outline-success" for="option6">or</label>
  53.         <input <?= (isset($opr) && ($opr == 'xor')) ? 'checked':''; ?> type="radio" class="btn-check" name="opr" id="option7" value="xor" autocomplete="off">
  54.         <label class="btn btn-outline-success" for="option7">xor</label><br><br>
  55.         <input name="submit" type="submit" class="btn btn-primary" value="Cek">
  56.     </form>
  57.     <?php if (isset($hasil)) { ?>
  58.         <div class="alert alert-success" role="alert">
  59.             <div class="form-check form-switch">
  60.                 <input <?= ($hasil) ? 'checked':'' ?> disabled class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault">
  61.                 <label class="form-check-label" for="flexSwitchCheckDefault">C</label><br>                
  62.             </div>
  63.             <?= (($a) ? 'true' : 'false').' '.$opr.' '.(($b)?'true':'false').' = '.(($hasil)?'true':'false'); ?>
  64.         </div>
  65.     <?php } else if (isset($error)) { ?>
  66.         <div class="alert alert-danger" role="alert">
  67.             <?= $error ?>
  68.         </div>    
  69.     <?php } ?>    
  70.     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
  71. </body>
  72. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement