一个简单的php实现计算器代码,哪里错了

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Calculator</title>
    <style>
        #container {
            width: 180px;
            margin-left: auto;
            margin-right: auto;
        }
        .input {
            width:100%
        }
        .text{
            text-align:center;
        }
         
    </style>

</head>
<body>
<div class="text">
    <h2>计算器</h2>
</div>
<div id = "container">
    <form method = 'post'>
            <input type="text" name = 'a'>
            <select name="ch">
                <option value="">+</option>
                <option value="">-</option>
                <option value="">*</option>
                <option value="">/</option>
            </select>
            <input type="text" name = 'b'>
            <p>answer:</p>
            <input type="submit" value="等于">
    </form>
</div>
    <?php
    @$a = $_POST['a'];
    @$b = $_POST['b'];
    @$ch = $_POST['ch'];

    function add($a,$b) {
        return $a + $b;
    }
    function sub($a,$b) {
        return $a - $b;
    }

    function mul($a,$b) {
        return $a * $b;
    }
    function div($a,$b) {
        return $a / $b;
    }
    $fun;
    switch ($ch) {
        case "+":
            $fun = 'add';
            break;
        case "-":
            $fun = 'sub';
            break;
        case "*":
            $fun = "mul";
            break;
        case "/":
            $fun = "div";
            break;
        default:
            break;
    }
    function getAnswer($t1,$t2,$callback) {
        return $callback($t1, $t2);
    }

    echo getAnswer($a, $b, $fun);
?>
</body>
</html>
慕雪6442864
浏览 463回答 2
2回答

HUWWW

option选项没有给value赋值 另外建议不要使用@,这是一个较差的习惯,影响性能不说,容错能力也不好建议你增加容错判断,你刚刚也就发现了值有问题了,对吧

小怪兽爱吃肉

<select name="ch"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select>
打开App,查看更多内容
随时随地看视频慕课网APP