PHP笔记3__简易计算器

<?php
    error_reporting(E_ALL & ~E_NOTICE);

    if( isset($_POST[‘sub‘]) ){
        $bz = true;
        $errs = "有以下问题:<br>";
        if($_POST[‘num1‘]==""){
            $bz = false;
            $errs .= "第一个数为空 <br>";
        }
        if($_POST[‘num2‘]==""){
            $bz = false;
            $errs .= "第二个数为空 <br>";
        }
        if($bz){
            $sum = "";
            switch($_POST[‘ysf‘]){
                case ‘+‘:
                    $sum = $_POST[‘num1‘] + $_POST[‘num2‘]; break;
                case ‘-‘:
                    $sum = $_POST[‘num1‘] - $_POST[‘num2‘]; break;
                case ‘*‘:
                    $sum = $_POST[‘num1‘] * $_POST[‘num2‘]; break;
                case ‘/‘:
                    $sum = $_POST[‘num1‘] / $_POST[‘num2‘]; break;
            }
        }
    }
?>

<html>
    <head>
        <title>Easy Calculator</title>
    </head>
    <body>
        <table border="1" width="400" align="center">
            <form action="index.php" method="post">
                <caption><h1>easy calculator</h1></caption>
                <tr>
                    <td>
                        <input type="text" size="5" name="num1" value="<?php echo $_POST[‘num1‘] ?>" /> <br>
                    </td>
                    <td>
                        <select name="ysf">
                            <option <?php if($_POST[‘ysf‘]=="+") echo "selected"; ?> value="+">+</option>
                            <option <?php if($_POST[‘ysf‘]=="-") echo "selected"; ?> value="-">-</option>
                            <option <?php if($_POST[‘ysf‘]=="*") echo "selected"; ?> value="*">*</option>
                            <option <?php if($_POST[‘ysf‘]=="/") echo "selected"; ?> value="/">/</option>
                        </select>
                    </td>
                    <td>
                        <input type="text" size="5" name="num2" value="<?php echo $_POST[‘num2‘] ?>" /> <br>
                    </td>
                    <td>
                        <input type="submit" name="sub" value="计算" /> <br>
                    </td>
                <tr>
                <tr>
                    <td colspan="4">
                        <?php
                            if($bz){
                                echo "计算结果:{$_POST[‘num1‘]} {$_POST[‘ysf‘]} {$_POST[‘num2‘]} = {$sum} <br>";
                            }
                            else{
                                echo $errs;
                            }
                        ?>
                    </td>
                </tr>
            </form>
        </table>
    </body>
</html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。