gurumutant icon

PHP - Demo Operator Logika

gurumutant | PRO | 03/26/24 01:51:28 AM UTC | 0 ⭐ | 1235 👁️ | Never ⏰ | []
PHP |

3.89 KB

|

None

|

0 👍

/

0 👎

<?php
    // mengecek pengiriman form
    if (isset($_POST['submit'])) {
        // echo "<pre>"; print_r($_POST); echo "</pre>"; 
        // mengekstraksi array $_POST menjadi variabel individual
        extract($_POST);
        // mengeset a dan b dari aktif tidaknya switcher
        $a = (isset($a)) ? true : false;
        $b = (isset($b)) ? true : false;
        // pengecekan pengguna memilih operator atau tidak
        if (isset($opr)) {
            // mendapatkan hasil berdasarkan operator yang dipilih
            switch ($opr) {
                case 'and': $hasil = ($a && $b); break;
                case 'or': $hasil = ($a || $b); break;
                case 'xor': $hasil = ($a xor $b); break;
                default: unset($hasil);
            }
        } else {
            $error = "Anda tidak memilih operator !";
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <title>Document</title>
</head>
<body>
    <form method="post">
        <div class="form-check form-switch">
            <input <?= isset($a) ? ($a?'checked':''):''; ?> name="a" value="1" class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault">
            <label class="form-check-label" for="flexSwitchCheckDefault">A</label>
        </div>
        <div class="form-check form-switch">
            <!-- <input <?= isset($b) ? ($b?'checked':''):''; ?> name="b" value="1" class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault"> -->
            <input <?php
                if (isset($b)) {
                    if ($b == true) {
                        echo 'checked';
                    }
                }
            ?> name="b" value="1" class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault">
            <label class="form-check-label" for="flexSwitchCheckDefault">B</label>
        </div>
        <input <?= (isset($opr) && ($opr == 'and')) ? 'checked':''; ?> type="radio" class="btn-check" name="opr" id="option5" value="and" autocomplete="off">
        <label class="btn btn-outline-success" for="option5">and</label>
        <input <?= (isset($opr) && ($opr == 'or')) ? 'checked':''; ?> type="radio" class="btn-check" name="opr" id="option6" value="or" autocomplete="off">
        <label class="btn btn-outline-success" for="option6">or</label>
        <input <?= (isset($opr) && ($opr == 'xor')) ? 'checked':''; ?> type="radio" class="btn-check" name="opr" id="option7" value="xor" autocomplete="off">
        <label class="btn btn-outline-success" for="option7">xor</label><br><br>
        <input name="submit" type="submit" class="btn btn-primary" value="Cek">
    </form>
    <?php if (isset($hasil)) { ?>
        <div class="alert alert-success" role="alert">
            <div class="form-check form-switch">
                <input <?= ($hasil) ? 'checked':'' ?> disabled class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault">
                <label class="form-check-label" for="flexSwitchCheckDefault">C</label><br>                
            </div>
            <?= (($a) ? 'true' : 'false').' '.$opr.' '.(($b)?'true':'false').' = '.(($hasil)?'true':'false'); ?> 
        </div>
    <?php } else if (isset($error)) { ?>
        <div class="alert alert-danger" role="alert">
            <?= $error ?>
        </div>    
    <?php } ?>    
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

Comments