PHP 简单计算器

 
 
<!—calculate.php—>
 
<html>
<head><title>计算器</title></head>

<body>

<form action="result.php" method="post">
<table border="1">
    <tr><td>第一个数</td><td><input type="text" name="num1"></input></td></tr>
    <tr><td>第二个数</td><td><input type="text" name="num2"></input></td></tr>
    <tr> 
        <td>运算符</td>
        <td>
            <select name="oper"> 
            <option value="+"></option>  
            <option value="-"></option>
            <option value="*"></option>
            <option value="/"></option>
        </td>
    </tr>
    <tr><td colspan="2"><input type="submit" value="结果"></input></td></tr>
</table>
<form>

</body>
</html>
 
 
<!—result.php—>

<?php
    $num1 = $_REQUEST['num1'];
    $num2 = $_REQUEST['num2'];
    $oper = $_REQUEST['oper'];
    
    $result = 0;
    
    switch ($oper) 
    {
        case "+":
            $result = $num1 + $num2;
            break;
        case "-":
            $result = $num1 - $num2;
            break;
        case "*":
            $result = $num1 * $num2;
            break;
        case "/":
            $result = $num1 / $num2;
            break;
    }
    
    echo "$result";
?>
    <a href="calculate.php">返回计算页面</a>



posted @ 2013-06-12 22:01  helloweworld  阅读(201)  评论(0编辑  收藏  举报