时间:2021-07-01 10:21:17 帮助过:169人阅读
用PHP写一个计算器
<body> <?php if (!empty($_POST)) { $op=$_POST['point']; $sum1 = $_POST['sum1']; $sum2 = $_POST['sum2']; $sum = 0; if ($sum1 != '' && is_numeric($sum1) && $sum2 != '' && is_numeric($sum2)) { switch ($op) { case '+': $sum = $sum1 + $sum2; break; case '-': $sum = $sum1 - $sum2; break; case '*': $sum = $sum1 * $sum2; break; case '/': $sum = $sum1 / $sum2; break; } echo <<<shi <script> window.onload = function() { document.getElementsByName('sum3')[0].setAttribute('value', '$sum') document.getElementsByName('sum1')[0].setAttribute('value', '$sum1') document.getElementsByName('sum2')[0].setAttribute('value', '$sum2') document.getElementsByName('{$op}')[0].setAttribute('selected','selected') } </script> shi; } else { echo '输入内容必须是数字'; } }; ?> <form action="" method='post'> <input type="text" name="sum1"> <select name='point'> <option name='+'>+</option> <option name='-'>-</option> <option name='*'>*</option> <option name='/'>/</option> </select> <input type="text" name='sum2'> <input type="submit" name='button' value='='> <input type="text" name='sum3'> </form> </body>
(免费学习视频教程分享:php视频教程)
以上就是用PHP写一个计算器(附完整代码)的详细内容,更多请关注Gxlcms其它相关文章!