点表示法在switch语句中不起作用

时间:2019-07-18 20:07:11

标签: php

我是这种php语言的新手,到目前为止,我知道点表示法已用于将变量与字符串连接起来。请告诉我以下代码为何无法正常工作。

            <form action="test.php" method="post">
                <label for="num1">First Number:</label>
                <input type="number" name="num1"><br>
                <input type="radio" name="operator[]" value="add">
                <label for="add">Add</label>
                <input type="radio" name="operator[]" value="subtract">
                <label for="subtract">Subtract</label>
                <input type="radio" name="operator[]" value="multiply">
                <label for="multiply">Multiply</label>
                <input type="radio" name="operator[]" value="divide">
                <label for="divide">Divide</label><br>
                <label for="num1">Second Number:</label>
                <input type="number" name="num2"><br>
                <input type="submit" value="Compute">
            </form> 
            <?php 
            $num1 = $_POST["num1"];
            $num2 = $_POST["num2"];
            $operators = $_POST["operator"];

            // var_export($operator);
            foreach ($operators as $operator => $value) {
                switch ($value) {
                    case 'add':
                        echo "Answer: ".$num1 + .$num2;
                        break;
                    case 'subtract':
                        echo "Answer: ".$num1 - .$num2;
                        break;
                    case 'multiply':
                        echo "Answer: ".$num1 * .$num2;
                        break;
                    case 'divide':
                        echo "Answer: ".$num1 / .$num2;
                        break;          
                    default:
                        return "out of bound";
                        break;
                }
            }
            ?>

0 个答案:

没有答案