PHP实现计算器小功能
吾爱主题
阅读:141
2021-10-25 14:10:00
评论:0
本文实例为大家分享了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 | <?php if (isset( $_POST [ 'sub' ])) { $f = true; $error = "有以上问题: <br>" ; if ( $_POST [ "num1" ] == "" ) { $f = false; $error .= "第一个数不能为空.<br>" ; } else { if (! is_numeric ( $_POST [num1])) { $f = false; $error .= "第一个数字不是数字.<br>" ; } } if ( $_POST [ 'num2' ] == "" ) { $f = false; $error .= "第二个数字不能为空.<br>" ; } else { if (! is_numeric ( $_POST [ 'num2' ])) { $f = false; $error .= "第二个数字不是数字<br>" ; } } if ( $f ) { 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 ; case '%' : $sum = $_POST [ 'num1' ]% $_POST [ 'num2' ]; break ; } } } ?> |
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 | <!doctype html> < html > < head > < meta charset = "utf-8" > < title >简易计算器</ title > < style > * { margin: 0px; padding: 0px; } #contains { width: 400px; margin: 50px auto; } h1 { width: 400px; text-align: center; color: #0F0; } table { margin-top: 20px; width: 400px; } input { background: yellow; border-radius: 20px; } input:hover { background: green; } select { background: yellow; border-radius: 20px; } td { background: #3F0; text-align: center; height: 30px; } #sub { border-radius: 25px; width: 50px; } </ style > </ head > < body > < div id = "contains" > < h1 >简易计算器</ h1 > < form action = "Calculator.php" method = "post" > < table border = "0" > < tr > < td >< input type = "text" name = "num1" value="<?php echo $_POST['num1'];?>" size="3"/></ 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 > < option <?php if($_POST['ysf'] == "%") echo "selected";?> value="%">%</ option > </ select > </ td > < td >< input type = "text" name = "num2" value="<?php echo $_POST['num2'];?>" size="3"/></ td > < td >< input type = "submit" id = 'sub' name = "sub" value = "计算" /></ td > </ tr > < tr > < td colspan = "4" > <? php if ($f) { echo "计算结果: <b>{$_POST['num1']}{$_POST['ysf']}{$_POST['num2']} = {$sum}</ b >"; } else { echo "< b >$error</ b >"; } ?> </ td > </ tr > </ table > </ form > </ div > </ body > </ html > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/shofe11/article/details/37101221
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。