用php做个简单的计算
内容摘要
这篇文章主要为大家详细介绍了用php做个简单的计算,具有一定的参考价值,可以用来参考一下。
文章正文
这篇文章主要为大家详细介绍了用php做个简单的计算,具有一定的参考价值,可以用来参考一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | <code><!DOCTYPE html> <html> <head> <title>PHP实现计算器</title> </head> <body> <?php $mess = "" ; if (isset( $_POST [ "sub" ])){ if ( $_POST [ "num1" ] == "" ){ $mess .= "第一个数不能为空!<br>" ; } else { if (! is_numeric ( $_POST [ "num1" ])){ $mess .= "第一个数必须是数字!<br>" ; } } if ( $_POST [ "num2" ]== "" ){ $mess .= "第二个数不能为空!<br>" ; } else { if (! is_numeric ( $_POST [ "num2" ])){ $mess .= "第二个数必须是数字!<br>" ; } else { if ( $_POST [ "opt" ]== "/" && $_POST [ "num2" ]==0){ $mess .= "除数不能为0" ; } } } } ?> <table border= "1" align= "center" width= "400" > <form action= "" method= "post" > <caption><h1>计算器</h1></caption> <tr> <td> <input type= "text" size= "4" name= "num1" value= "<?php echo $_POST[" num1 "]?>" /> </td> <td> <select name= "opt" > <option value= "+" <?php echo $_POST [ "opt" ]== "+" ? "selected" : "" ?>>+</option> <option value= "-" <?php echo $_POST [ "opt" ]== "-" ? "selected" : "" ?>>-</option> <option value= "x" <?php echo $_POST [ "opt" ]== "x" ? "selected" : "" ?>>x</option> <option value= "/" <?php echo $_POST [ "opt" ]== "/" ? "selected" : "" ?>>/</option> <option value= "%" <?php echo $_POST [ "opt" ]== "%" ? "selected" : "" ?>>%</option> </select> </td> <td> <input type= "text" size= "4" name= "num2" value= "<?php echo $_POST[" num2 "]?>" /> </td> <td> <input type= "submit" name= "sub" value= "计算" /> </td> </tr> </form> <?php if (isset( $_POST [ "sub" ])){ echo '<tr><td colspan="4">' ; if (! $mess ){ $sum =0; switch ( $_POST [ "opt" ]){ case "+" : $sum = $_POST [ "num1" ] + $_POST [ "num2" ]; break ; case "-" : $sum = $_POST [ "num1" ] - $_POST [ "num2" ]; break ; case "x" : $sum = $_POST [ "num1" ] * $_POST [ "num2" ]; break ; case "/" : $sum = $_POST [ "num1" ] / $_POST [ "num2" ]; break ; case "%" : $sum = $_POST [ "num1" ] % $_POST [ "num2" ]; break ; } echo "结果:{$_POST['num1']} {$_POST['opt']} {$_POST['num2']} = {$sum}" ; } else { echo $mess ; } echo '</td></tr>' ; } ?> </table> </body> </html></code> |
注:关于用php做个简单的计算的内容就先介绍到这里,更多相关文章的可以留意
代码注释